# [Numpy-discussion] Numpy performance vs Matlab.

Nicolas ROUX nicolas.roux@st....
Wed Jan 7 09:19:16 CST 2009

```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 ?