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.

### Like this:

Like Loading...

*Related*

This entry was posted on April 11, 2007 at 12:06 pm and is filed under programming.

April 11, 2007 at 12:27 pm

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

April 11, 2007 at 1:45 pm

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

April 11, 2007 at 6:32 pm

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

April 18, 2007 at 11:35 am

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.

September 7, 2007 at 5:05 pm

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

December 20, 2007 at 8:00 am

I would like to see a continuation of the topic

February 29, 2008 at 9:28 am

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

March 6, 2008 at 7:28 pm

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

March 6, 2008 at 10:03 pm

Ever tried Scilab?

March 12, 2008 at 5:36 pm

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

July 29, 2008 at 2:54 pm

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

July 29, 2008 at 3:41 pm

I used Matlab 6.5, iirc. No JIT compiler.

November 14, 2008 at 9:51 pm

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

October 10, 2009 at 10:48 am

Mathematics of Linear Algebra

February 24, 2010 at 2:54 am

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

May 28, 2010 at 8:17 am

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

June 2, 2010 at 8:57 am

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