42

I want to compute magnetic fields of some conductors using the Biot–Savart law and I want to use a 1000x1000x1000 matrix. Before I use MATLAB, but now I want to use Python. Is Python slower than MATLAB ? How can I make Python faster?

EDIT: Maybe the best way is to compute the big array with C/C++ and then transfering them to Python. I want to visualise then with VPython.

EDIT2: Which is better in my case: C or C++?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kame
  • 20,848
  • 33
  • 104
  • 159
  • 4
    It's difficult to estimate speed without code. If you have some Python code that works you can post it again for tips on speedup. – extraneon Jan 25 '10 at 15:05
  • 1
    Matlab is normally ridiculously good about optimizing matrix computations – BlueRaja - Danny Pflughoeft Jan 25 '10 at 15:15
  • @Blue: The individual computations should run pretty fast. But we don't know how well Biot-Savart maps to Matlab computations. If it's not well-suited to vectorization, it will get rather slow. – Mike DeSimone Jan 25 '10 at 16:13
  • 7
    Of course, you must realize that this will require you make at least one matrix that has 10^9 elements. If they are all doubles, then this is about 8 gigabytes of memory. No matter what you do, this will take some time. –  Jan 25 '10 at 16:37
  • 1
    At my Physics department, we mostly do C/C++ or FORTRAN, but some use Python as kind of "frontend" to C, which is quite a nice way to go IMHO. I'd go for the Python+C approach. – Boldewyn Jan 25 '10 at 17:23
  • 9
    Do not use C/C++ if you never used them before. It would be an exercise in frustration. – Meh Jan 26 '10 at 08:18

8 Answers8

28

You might find some useful results at the bottom of this link

http://wiki.scipy.org/PerformancePython

From the introduction,

A comparison of weave with NumPy, Pyrex, Psyco, Fortran (77 and 90) and C++ for solving Laplace's equation.

It also compares MATLAB and seems to show similar speeds to when using Python and NumPy.

Of course this is only a specific example, your application might be allow better or worse performance. There is no harm in running the same test on both and comparing.

You can also compile NumPy with optimized libraries such as ATLAS which provides some BLAS/LAPACK routines. These should be of comparable speed to MATLAB.

I'm not sure if the NumPy downloads are already built against it, but I think ATLAS will tune libraries to your system if you compile NumPy,

http://www.scipy.org/Installing_SciPy/Windows

The link has more details on what is required under the Windows platform.

EDIT:

If you want to find out what performs better, C or C++, it might be worth asking a new question. Although from the link above C++ has best performance. Other solutions are quite close too i.e. Pyrex, Python/Fortran (using f2py) and inline C++.

The only matrix algebra under C++ I have ever done was using MTL and implementing an Extended Kalman Filter. I guess, though, in essence it depends on the libraries you are using LAPACK/BLAS and how well optimised it is.

This link has a list of object-oriented numerical packages for many languages.

http://www.oonumerics.org/oon/

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
petantik
  • 1,072
  • 8
  • 12
21

NumPy and MATLAB both use an underlying BLAS implementation for standard linear algebra operations. For some time both used ATLAS, but nowadays MATLAB apparently also comes with other implementations like Intel's Math Kernel Library (MKL). Which one is faster by how much depends on the system and how the BLAS implementation was compiled. You can also compile NumPy with MKL and Enthought is working on MKL support for their Python distribution (see their roadmap). Here is also a recent interesting blog post about this.

On the other hand, if you need more specialized operations or data structures then both Python and MATLAB offer you various ways for optimization (like Cython, PyCUDA,...).

Edit: I corrected this answer to take into account different BLAS implementations. I hope it is now a fair representation of the current situation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nikow
  • 21,190
  • 7
  • 49
  • 70
  • You should elaborate what you mean by "the same underlying numeric libraries." Matlab is closed source and has been around longer than Python, much less NumPy, so I'm having a hard time thinking what could be common ground beyond, say, the SSE instruction set. – Mike DeSimone Jan 25 '10 at 16:12
  • 2
    Upvoted. No-one (I exaggerate) writes matrix manipulation routines, everyone calls them. Of course, someone has to write them in the first place, but if you were writing Matlab or SciPy you'd find a screamingly-fast implementation of BLAS for your target platform(s) and call that. I'll be very surprised to learn that one is significantly faster than the other. – High Performance Mark Jan 25 '10 at 16:13
  • @Mike: This is pretty common knowledge, but for people with no experience in scientific computing I added some more information in my answer. – nikow Jan 25 '10 at 16:27
7

The only valid test is to benchmark it. It really depends on what your platform is, and how well the Biot-Savart Law maps to Matlab or NumPy/SciPy built-in operations.

As for making Python faster, Google's working on Unladen Swallow, a JIT compiler for Python. There are probably other projects like this as well.

Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
  • But could you make a rough estimation? – kame Jan 25 '10 at 14:54
  • 1
    Not really. Deep down, if everything is done right, both of them will be running vectorized C code. I don't know how well Matlab is optimized (no access to the code) or NumPy/SciPy (haven't looked) in terms of using SSE (or equivalent) instructions. I also don't know how much faster Unladen Swallow will make the Python code. There's also Matlab's preference for storing data in `double` precision; you don't tell us whether your Python code is using `float` or `double` or something else. In short, this is a classic case of insufficient data. Thus, "The only valid test is combat." – Mike DeSimone Jan 25 '10 at 16:09
  • My experience in this area is over six years ending seven years ago. Back then, I used libraries provided by several vendors (Intel MKL was one of them). Matlab's insistence on using `double` at the time (they had only recently started supporting unsigned 8-bit) meant that we had to write some algorithms in C or C++ to speed them up. So I wrote a library as a layer over the various implementations so our devs would have only one API to deal with. Some algorithms were still too slow and had to be rewritten in assembly. That said, I don't see where my answer was misleading. – Mike DeSimone Jan 26 '10 at 19:33
5

As per your edit 2, I recommend very strongly that you use Fortran because you can leverage the available linear algebra subroutines (Lapack and Blas) and it is way simpler than C/C++ for matrix computations.

If you prefer to go with a C/C++ approach, I would use C, because you presumably need raw performance on a presumably simple interface (matrix computations tend to have simple interfaces and complex algorithms).

If, however, you decide to go with C++, you can use the TNT (the Template Numerical Toolkit, the C++ implementation of Lapack).

Good luck.

Escualo
  • 40,844
  • 23
  • 87
  • 135
3

If you're just using Python (with NumPy), it may be slower, depending on which pieces you use, whether or not you have optimized linear algebra libraries installed, and how well you know how to take advantage of NumPy.

To make it faster, there are a few things you can do. There is a tool called Cython that allows you to add type declarations to Python code and translate it into a Python extension module in C. How much benefit this gets you depends a bit on how diligent you are with your type declarations - if you don't add any at all, you won't see much of any benefit. Cython also has support for NumPy types, though these are a bit more complicated than other types.

If you have a good graphics card and are willing to learn a bit about GPU computing, PyCUDA can also help. (If you don't have an nvidia graphics card, I hear there is a PyOpenCL in the works as well). I don't know your problem domain, but if it can be mapped into a CUDA problem then it should be able to handle your 10^9 elements nicely.

onnodb
  • 5,241
  • 1
  • 32
  • 41
kenm
  • 23,127
  • 2
  • 43
  • 62
2

And here is an updated "comparison" between MATLAB and NumPy/MKL based on some linear algebra functions:

http://dpinte.wordpress.com/2010/03/16/numpymkl-vs-matlab-performance/

The dot product is not that slow ;-)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

I couldn't find much hard numbers to answer this same question so I went ahead and did the testing myself. The results, scripts, and data sets used are all available here on my post on MATLAB vs Python speed for vibration analysis.

Long story short, the FFT function in MATLAB is better than Python but you can do some simple manipulation to get comparable results and speed. I also found that importing data was faster in Python compared to MATLAB (even for MAT files using the scipy.io).

Steve
  • 289
  • 3
  • 3
0

I would also like to point out that Python (+NumPy) can easily interface with Fortran via the F2Py module, which basically nets you native Fortran speeds on the pieces of code you offload into it.

rayhem
  • 741
  • 1
  • 8
  • 23