[Numpy-discussion] Numpy performance vs Matlab.

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Jan 7 11:36:27 EST 2009


On Wed, Jan 7, 2009 at 10:58 AM, Grissiom <chaos.proton at gmail.com> wrote:
> On Wed, Jan 7, 2009 at 23:44, Ryan May <rmay31 at gmail.com> wrote:
>>
>> 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?
>>
>
> Yes, I think so. I think the testcase  is a matter of python loop vs matlab
> loop rather than python vs matlab.
>
> --
> Cheers,
> Grissiom
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>

I tried with matlab 2006a, I don't know if there is JIT, but the main
speed difference comes with the numpy array access.

The test is actually biased in favor of python, since in the matlab
code the initialization with zeros is inside the time count, but
outside in the python version

If I just put b=1.0 inside the double loop (no numpy)

Python  1.453644 sec
matlab  0.335249 seconds, with zeros outside loop:  0.060582 seconds

with original array assignment:

python/numpy 32.745030 sec
matlab             1.633415 seconds, with zeros outside loop: 1.251597 seconds

(putting the loop in a function and using psyco reduces speed by 30%)


Josef



More information about the NumPy-Discussion mailing list