C++ or Python for an Extensive Math Program?

Question:

I’m debating whether to use C++ or Python for a largely math-based program.

Both have great math libraries, but which language is generally faster for complex math?

Asked By: user1125551

||

Answers:

I guess it is safe to say that C++ is faster. Simply because it is a compiled language which means that only your code is running, not an interpreter as with python.

It is possible to write very fast code with python and very slow code with C++ though. So you have to program wisely in any language!

Another advantage is that C++ is type safe, which will help you to program what you actually want.

A disadvantage in some situations is that C++ is type safe, which will result in a design overhead. You have to think (maybe long and hard) about function and class interfaces, for instance.

I like python for many reasons. So don’t understand this a plea against python.

Answered By: steffen

You could also consider a hybrid approach. Python is generally easier and faster to develop in, specially for things like user interface, input/output etc.

C++ should certainly be faster for some math operations (although if your problem can be formulated in terms of vector operations or linear algebra than numpy provides a python interface to very efficient vector manipulations).

Python is easy to extend with Cython, Swig, Boost Python etc. so one strategy is write all the bookkeeping type parts of the program in Python and just do the computational code in C++.

Answered By: robince

Use the one you like better (and you should like python better :)).

In either case, any math-intensive computations should be carried out by existing libraries – which aren’t language dependent (usually BLAS / LAPACK are used to perform the actual math).
If you choose to go with python, use numpy
for calculations.

Edit: From your comments, it seems that you are very concerned with the speed of your program. The only way to know for sure how much time is wasted by the high level pythonic code is to profile your program (for example, use ipython with run -p).

In most cases, you will find that the high level stuff takes about 10% of the total running time, and therefore switching from python to C++ will only improve that 10% by some factor, for a total gain of perhaps 5% in running time.

Answered By: Guy Adini

It goes witout saying that C++ is going to be faster for intensive numeric computations. However, there are so many pre-existing libraries out there (written in C/C++/Haskell etc..), with Python wrappers – it’s just more convenient to utilise the convenience of Python and let the existing libraries carry the load.

One comprehensive system is http://www.sagemath.org and a fairly interesting link is the components it uses at http://sagemath.org/links-components.html.

A system with numpy/scipy and pandas from my experience is normally sufficient for most things.

Answered By: Jon Clements

It all depends if faster is “faster to execute” or “faster to develop”. Overall, python will be quicker for development, c++ faster for execution. For working with integers (arithmetic), it has full precision integers, it has a lot of external tools (numpy, pylab…) My advice would be go python first, if you have performance issue, then switch to cpp (or use external libraries written in cpp from python, in an hybrid approach)

There is no good answer, it all depends on what you want to do in terms of research / calculus

Answered By: Bruce

I sincerely doubt that Google and Stanford don’t know C++.

“Generally faster” is more than just language. Algorithms can make or break a solution, regardless of what language it’s written in. A poor choice written in C++ and be beaten by Java or Python if either makes a better algorithm choice.

For example, an in-memory, single CPU linear algebra library will have its doors blown in by a parallelized version done properly.

An implicit algorithm may actually be slower than an explicit one, despite time step stability restrictions, because the latter doesn’t have to invert a matrix. This is often true for hyperbolic partial differential equations.

You shouldn’t care about “generally faster”. You ought to look deeply into the problem you’re trying to solve and the algorithms used to solve it. You’ll do better that way than a blind language choice.

Answered By: duffymo

I would go with Python running on Java platform. This approach is implemented in DataMelt program. Algorithm that call Java libraries from Python can be faster, since JVM optimizes the code for you.

Answered By: Steve21
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.