Are there any useful resources to compare the performance of python and c++ languages in algorithms dedicated to solving discrete optimization problems?
-
3$\begingroup$ I think that without a doubt, C++ will perform better in terms of computation time, but Python will be much much faster to implement. The question is, is C++ what you need, given the amount of time you have, and given the project's requirements ? If you go with C++ or Python, what do you risk ? Can you do a proof of concept with Python for quick results, and then build on that, with a switch to C++ ? $\endgroup$– KuifjeCommented Apr 13, 2022 at 13:28
-
2$\begingroup$ Having some information about the additional computation time required by a Python implementation is clearly valuable when weighing between C++ and Python. It's not the same if its twice slower, or 100 times slower. Python being much faster to implement is also debatable :-) $\endgroup$– fontanfCommented Apr 13, 2022 at 15:29
-
3$\begingroup$ @Kuifje: C++'s problem is not so much that things take longer to implement in C++. It's more along the lines of people learning C++ getting taught C first (which I consider a grieveous mistake), and then getting stuck halfway between C and C++ in how they implement things. This is IMHO the source of the tons of sub-par C++ code out there (being half C instead of actually utilizing the language) that is then used to showcase how "complicated" C++ solutions to a problem are. $\endgroup$– DevSolarCommented Apr 14, 2022 at 10:40
-
1$\begingroup$ I do not doubt that an expert in C++ can implement things fast. But in average, it is safe to say that C++ will require more lines of code. The following well known comic is a bit caricatural, but not completely false :D twitter.com/sadatrahman/status/456587828869074944 $\endgroup$– KuifjeCommented Apr 14, 2022 at 10:47
-
$\begingroup$ @Kuifje: If one line of code does not solve your problem, your line isn't long enough. $\endgroup$– DevSolarCommented Apr 14, 2022 at 17:06
2 Answers
If you are using a solver (open-source or commercial) to solve a discrete optimization problem, and if the problem is not trivial or extremely easy, chances are very high that the bulk of the computation time will be spent inside the solver. The solver will almost assured be programmed in C or C++. So the Python/C++ question likely boils down to which performs better (and by how much) when constructing the model and recovering and post-processing the solution. C++ will almost certainly be faster, but whether the difference in speed matters to you will probably depend on how large the model is and how sensitive you are to the time spent setting up the model (which, on a difficult model, will probably be a very small fraction of the total solution time).
-
2$\begingroup$ Something to consider is that the person responsible for setting up the problem and further analyzing the results is not always going to be a "programmer". The chance that this person is more comfortable with Python compared to C(++) is probably pretty high. $\endgroup$ Commented Apr 15, 2022 at 0:08
-
$\begingroup$ @noslenkwah Very true ... and might argue for using a domain specific modeling language to make the model more readable. I suspect a non-programmer would find a model in a DSL (AMPL, GAMS, OPL, ...) considerably easier to read than either Python or C/C++ code. $\endgroup$– prubin ♦Commented Apr 15, 2022 at 15:29
However you measure it, you'll find that the C++ is faster, but almost certainly by a linear, or sub-linear, factor.
So if something takes two or three times as long to solve, does it really matter?
Especially when CPUs continue to be faster and cheaper each year.
I'd be more concerned about the efficiency of the algorithm itself, where cutting something down from n³ to n² will more than make up for any inefficiencies of the specific language.
Well-written, simple, and obvious code:
- Provides confidence that it is correct.
- Greatly reduces the human time required for coding, debugging, maintenance, and future modification.
-
3$\begingroup$ Be careful with that statement "2-3x slower". That is not even close to true in some cases. Python can be much slower than C depending on what you are doing. In most cases, when performance matters, Python will call out to some package that was written in C/C++. So, you can still get the best of both worlds. $\endgroup$ Commented Apr 18, 2022 at 18:15