[Numpy-discussion] Numpy performance vs Matlab.

Ryan May rmay31@gmail....
Wed Jan 7 09:44:50 CST 2009


Nicolas ROUX wrote:
> Hi,
> 
> I need help ;-)
> I have here a testcase which works much faster in Matlab than Numpy.
>  
> The following code takes less than 0.9sec in Matlab, but 21sec in Python.
> Numpy is 24 times slower than Matlab !
> The big trouble I have is a large team of people within my company is ready to replace Matlab by Numpy/Scipy/Matplotlib,
> but I have to demonstrate that this kind of Python Code is executed with the same performance than Matlab, without writing C extension.
> This is becoming a critical point for us.
> 
> This is a testcase that people would like to see working without any code restructuring.
> The reasons are:
> - this way of writing is fairly natural.
> - the original code which showed me the matlab/Numpy performance differences is much more complex,
> and can't benefit from broadcasting or other numpy tips (I can later give this code)
> 
> ...So I really need to use the code below, without restructuring.
> 
> Numpy/Python code:
> #####################################################################
> import numpy
> import time
> 
> print "Start test \n" 
> 
> dim = 3000
> 
> a = numpy.zeros((dim,dim,3))
> 
> start = time.clock()
> 
> for i in range(dim):
>     for j in range(dim):
>         a[i,j,0] = a[i,j,1]
>         a[i,j,2] = a[i,j,0]
>         a[i,j,1] = a[i,j,2]
> 
> end = time.clock() - start        
> 
> print "Test done,   %f sec" % end
> #####################################################################
<SNIP>
> Any idea on it ?
> Did I missed something ?

I think you may have reduced the complexity a bit too much.  The python code
above sets all of the elements equal to a[i,j,1].  Is there any reason you can't
use slicing to avoid the loops?

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma


More information about the Numpy-discussion mailing list