Hello all,
Can someone please explain to me why the following happens. I know that I am accessing the array in the wrong order, it is necessary to reproduce the allocation and slowdown. This came up in an actual application and I could not figure out why:
function f1(A)
n1=size(A,1)
n2=size(A,2)
s=0.
for i=1:n1,j=1:n2-1:n2
s+=A[i,j]
end
return s
end
function f2(A)
n1=size(A,1)
n2=size(A,2)
s=0.
for i=1:n1, j in [1,n2]
s+=A[i,j]
end
return s
end
n=10;A=rand(n,n)
f1(A);f2(A);
n=10000;A=rand(n,n)
@time s1=f1(A);
@time s2=f2(A);
s1-s2
0.000608 seconds (5 allocations: 176 bytes)
0.001324 seconds (20.00 k allocations: 1.068 MB)
0.0
Thanks!