Hi Joaquim,
First, you need to call the functions once before timing them,
otherwise the compilation time is also included.
Second, N is a global variable in your example, which is slow.
If you want to use N as a global variable, make it a constant with
const N = 100
this will increase the speed of all functions and also the difference
between the various implementations.
Otherwise you can put inside every function something like
N = size(x,1)
Then your last function definition has an argument xx, instead of y.
And finally, you actually do not need to specify all the function
parameters the way you did in order to get speed.
The following will also do:
const N=100
x=rand(N,N,N)
y=rand(N,N,N)
function abc(x,y)
for a=1:N
for b=1:N
for c=1:N
y[a,b,c]=x[a,b,c]
end
end
end
end
function acb(x,y)
for a=1:N
for c=1:N
for b=1:N
y[a,b,c]=x[a,b,c]
end
end
end
end
function bac(x,y)
for b=1:N
for a=1:N
for c=1:N
y[a,b,c]=x[a,b,c]
end
end
end
end
function bca(x,y)
for b=1:N
for c=1:N
for a=1:N
y[a,b,c]=x[a,b,c]
end
end
end
end
function cab(x,y)
for c=1:N
for a=1:N
for b=1:N
y[a,b,c]=x[a,b,c]
end
end
end
end
function cba(x,y)
for c=1:N
for b=1:N
for a=1:N
y[a,b,c]=x[a,b,c]
end
end
end
end
@time abc(x,y)
@time acb(x,y)
@time bac(x,y)
@time bca(x,y)
@time cab(x,y)
@time cba(x,y)
println()
@time abc(x,y)
@time acb(x,y)
@time bac(x,y)
@time bca(x,y)
@time cab(x,y)
@time cba(x,y)
cheers,
andre