# [Numpy-discussion] Numpy performance vs Matlab.

josef.pktd@gmai... josef.pktd@gmai...
Wed Jan 7 11:27:07 CST 2009

```A test case closer to my applications is calling functions in loops:

Python
-----------------------------------
def assgn(a,i,j):
a[i,j,0] = a[i,j,1] + 1.0
a[i,j,2] = a[i,j,0]
a[i,j,1] = a[i,j,2]
return a

print "Start test \n"

dim = 300#0

a = numpy.zeros((dim,dim,3))
start = time.clock()

for i in range(dim):
for j in range(dim):
assgn(a,i,j)

end = time.clock() - start
assert numpy.max(a)==1.0  #added to check inplace substitution
print "Test done,   %f sec" % end
---------------------------

matlab:
------------------------------------------------------------------
function a = tryloopspeed()

'Start test'
dim = 300;
a = zeros(dim,dim,3);
tic;
for i = 1:dim
for j = 1:dim
a = assgn(a,i,j);
end
end
toc
'Test done'
end
function a = assgn(a,i,j)
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
---------------------------

Note: I had to reduce the size of the matrix because I got impatient
waiting for matlab
time:

python: Test done,   0.486127 sec
matlab:
>> output = tryloopspeed();
ans =
Start test
Elapsed time is 511.815971 seconds.
ans =
Test done
>> 511.815971/60.0  #minutes
ans =
8.530

matlab takes 1053 times the time of python
The problem is that at least in my version of matlab, it copies
function arguments when they are modified. It's possible to work
around this, but not very clean.

So for simple loops python looses, but for other things, python wins
by a huge margin. Unless somebody can spot a mistake in my timing.

Josef
```