## 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).

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). 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.

### 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 : https://lbolla.wordpress.com/2007/04/11/numerical-computing-matlab-vs-pythonnumpyweave/ 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. lorenzo Says:

other benchmarks specifically on linear algebra can be found here.

https://www.osc.edu/blogs/index.php/sip?blog=7&p=37&page=1&more=1&c=1&tb=1&pb=1&disp=single

6. Maximus Says:

I would like to see a continuation of the topic

7. […] 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 […]

8. Rob Steele Says:

Been there done that. Try R (http://www.r-project.org/). I’ve never been more productive.

9. Casey Says:

Ever tried Scilab?

10. Adolph Says:

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

11. Memming Says:

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

12. lbolla Says:

I used Matlab 6.5, iirc. No JIT compiler.

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

14. itsrsu Says:

Mathematics of Linear Algebra

15. isabel Says:

hi i have laplace.py, 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

16. Kitty Says:

numerical computing: matlab vs python+numpy+weave ? lorenzo bolla’s blogguy@gigemail.net

17. Carlton Says:

numerical computing: matlab vs python+numpy+weave ? lorenzo bolla’s blog guy@gigemail.net