# [Numpy-discussion] Numpy performance vs Matlab.

Grissiom chaos.proton@gmail....
Wed Jan 7 09:58:40 CST 2009

```On Wed, Jan 7, 2009 at 23:44, Ryan May <rmay31@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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20090107/f9494024/attachment.html
```