[Numpy-discussion] Numpy performance vs Matlab.
David Cournapeau
david@ar.media.kyoto-u.ac...
Wed Jan 7 09:16:46 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
> #####################################################################
>
> Matlab code:
> #####################################################################
> 'Start test'
> dim = 3000;
> tic;
> a =zeros(dim,dim,3);
> for i = 1:dim
> for j = 1:dim
> a(i,j,1) = a(i,j,2);
> a(i,j,2) = a(i,j,1);
> a(i,j,3) = a(i,j,3);
> end
> end
> toc
> 'Test done'
> #####################################################################
>
> Any idea on it ?
> Did I missed something ?
>
I think on recent versions of matlab, there is nothing you can do
without modifying the code: matlab has some JIT compilation for loops,
which is supposed to speed up those cases - at least, that's what is
claimed by matlab. The above loops are typical examples where this
should work reasonably well I believe:
http://www.mathworks.com/access/helpdesk_r13/help/techdoc/matlab_prog/ch7_pe10.html
If you really have to use loops, then matlab will be faster. But maybe
you don't; can you show us a more typical example ?
cheers,
David
More information about the Numpy-discussion
mailing list