numerical computing: matlab vs python+numpy+weave

April 11, 2007

recently, I’ve discovered the power of python for numerical computing. being a slave of matlab for many years, I’ve decided to give python (and it’s numerical module numpy) a try, comparing its numerical crounching capabilities versus matlab’s.

the tests are heavily insipired by Prabhu Ramachandran. I’ve only added matlab to complete his comparisons. Here is a short description of the test, taken from here:
The example we will consider is a very simple (read, trivial) case of solving the 2D Laplace equation using an iterative finite difference scheme (four point averaging, Gauss-Seidel or Gauss-Jordan). The formal specification of the problem is as follows. We are required to solve for some unknown function u(x,y) such that \nabla^2 u = 0 with a boundary condition specified. For convenience the domain of interest is considered to be a rectangle and the boundary values at the sides of this rectangle are given.

all the tests have been run on a Pentium IV Xeon 2GHz, with 1Gb of RAM.
here is the matlab script I used laplace.m (it’s a doc file: sorry for that, but I’m only allowed to upload docs and images on this blog).

time spent by different solvers

matlab and numpy have pretty much the same performances. any form of optimization on python code does better than matlab. only the simple python (without numpy) is slower than matlab (note the logarithmic y scale to fit also the “slow” algorithm).

speed factor with respect to matlab

blitz is roughly twice as fast as matlab. inline, fastinline, fortran and pyrex only differ appreciably for small grids: for 500×500 grids they are around 10x faster than matlab.

here is some other digits, not included in the graphs:

  • numpy with psyco runs 5% faster than numpy alone;
  • weave and psyco (blitz, inline and fastinline) fails with following polite error: local variables of functions run by Psyco cannot be accessed in any way, sorry;
  • octave runs twice as slow as numpy and matlab.

pretty amazing, if you think that optimizing python, most of the time, is really trivial. moreover, python is free and easily parallelizable with MPI (I’ll show some performances in a future post).

tests have been run also on an SGI Altix with Itanium2 processors, on a single CPU. the graphs are reported in the following thumbnails.

time spent by different solversspeed factor with respect to matlab

17 Responses to “numerical computing: matlab vs python+numpy+weave”

  1. […] capacités de Matlab et de Python sur une application donnée. Le billet se trouve ici : Ce qui est surprenant, mais en fait pas tant que cela, c’est que Matlab et Python avec numpy font […]

  2. lbolla Says:

    thanks to David Joyner for correcting a typo in the post!

  3. Michael Lerner Says:

    What happens if you use psyco under the same conditions as blitz?

  4. lbolla Says:

    psyco used with weave gives a polite error: “local variables of functions run by Psyco cannot be accessed in any way, sorry.”
    I guess this should be fixed, somehow, but I don’t know if it worths the time: numpy+psyco runs just a 5% faster than numpy alone.

  5. Maximus Says:

    I would like to see a continuation of the topic

  6. […] scipy, matplotlib, and ipython. When combined with the appropriate libraries, Python can have better numerical performance than Matlab or Octave, nearly identical functionality, and the full […]

  7. Rob Steele Says:

    Been there done that. Try R ( I’ve never been more productive.

  8. Casey Says:

    Ever tried Scilab?

  9. Adolph Says:

    I would like to see times for grid sizes 600 to 900?

  10. Memming Says:

    What was the version of MATLAB? Did it have JIT compiler on?

  11. lbolla Says:

    I used Matlab 6.5, iirc. No JIT compiler.

  12. […] more detailed comparison based on this same example can be found here. A multidimensional example can be found […]

  13. isabel Says:

    hi i have, but when i run it it has error i want to know how can i do? or if you can say me how did you install the program, i hace python 2.6 for ubuntu

  14. Kitty Says:

    numerical computing: matlab vs python+numpy+weave ? lorenzo bolla’s

  15. Carlton Says:

    numerical computing: matlab vs python+numpy+weave ? lorenzo bolla’s blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: