spacing=1.5;
Nxx = 300 ;
Naa = 350;
Nalal = 200;
sigma = 10 ;
NoIter = 10000;
xx=NaN(Nxx,1);
xmin = 0.01;
xmax = 400;
xx(1) = xmin;
for i=2:Nxx
xx(i) = xx(i-1) + (xmax-xx(i-1))/((Nxx-i+1)^spacing);
end
f_util = @(c) c.^(1-sigma)/(1-sigma);
W=NaN(Nxx,1);
W(:,1) = f_util(xx);
W_temp = NaN(Nalal,Naa);
xprime = NaN(Nalal,Naa);
tic
for banana=1:NoIter
% tic
xprime=ones(Nalal,Naa);
W_temp=interp1(xx,W(:,1),xprime,'spline');
% toc
end
tocinclude("mnspline.jl")
spacing=1.5
Nxx = 300
Naa = 350
Nalal = 200
sigma = 10
NoIter = 10000
xx=Array(Float64,Nxx)
xmin = 0.01
xmax = 400
xx[1] = xmin
for i=2:Nxx
xx[i] = xx[i-1] + (xmax-xx[i-1])/((Nxx-i+1)^spacing)
end
f_util(c) = c.^(1-sigma)/(1-sigma)
W=Array(Float64,Nxx,1)
W[:,1] = f_util(xx)
spl = mnspline(xx,W[:,1])
function performance(NoIter::Int64)
W_temp = Array(Float64,Nalal*Naa)
W_temp2 = Array(Float64,Nalal,Naa)
xprime=Array(Float64,Nalal,Naa)
for banana=1:NoIter
xprime=ones(Nalal,Naa)
W_temp = spl(xprime[:])
end
W_temp2 = reshape(W_temp,Nalal,Naa)
end
@time performance()
0.000565 seconds (13 allocations: 1.603 MB)
MATLAB on the other hand: Elapsed time is 0.007864 seconds.
The bottom line being that in Julia the code is much faster (more than 10 times in this case), which should be since I use all my threads and the method is written in C. However, if I don't comment out the loop and run the code as posted above:
Julia:
3.205262 seconds (118.99 k allocations: 15.651 GB, 14.08% gc time)
MATLAB:
Elapsed time is 4.514449 seconds.
If I run the whole loop apparently MATLAB catches up a lot. It is still slower in this case. In the original problem though Julia was only about 3 times faster within the loop and once I looped through MATLAB turned out be 3 times faster. I am stuck here, does anybody have an idea what might be going? / If I am doing something wrong in my Julia code? A hint what I could do better?
Right now I am trying to parallelize also the loop. But that's obviously unfair compared with MATLAB because you could also parallelize that (If you buy that expensive toolbox).
function performance(NoIter::Int64) W_temp = Array(Float64,Nalal*Naa) W_temp2 = Array(Float64,Nalal,Naa) xprime=Array(Float64,Nalal,Naa) xprime=ones(Nalal,Naa) xprime = xprime[:] for banana=1:NoIter W_temp = spl(xprime) end W_temp2 = reshape(W_temp,Nalal,Naa)end// T
using Dierckx
spacing=1.5
Nxx = 300
Naa = 350
Nalal = 200
sigma = 10
NoIter = 10000
xx=Array(Float64,Nxx)
xmin = 0.01
xmax = 400
xx[1] = xmin
for i=2:Nxx
xx[i] = xx[i-1] + (xmax-xx[i-1])/((Nxx-i+1)^spacing)
end
f_util(c) = c.^(1-sigma)/(1-sigma)
W=Array(Float64,Nxx,1)
W[:,1] = f_util(xx)
spl = Spline1D(xx,W[:,1])
function performance2(NoIter::Int64)
W_temp = Array(Float64,Nalal*Naa)
W_temp2 = Array(Float64,Nalal,Naa)
xprime=Array(Float64,Nalal,Naa)
for banana=1:NoIter
xprime=ones(Nalal,Naa)
W_temp = spl(xprime[:])
end
W_temp2 = reshape(W_temp,Nalal,Naa)
end
@time performance2(10000)
30.878093 seconds (100.01 k allocations: 15.651 GB, 2.19% gc time)
Numerical Recipes in C: The Art of Scientific Computing, 2nd edition.
If all code run as it shown in the example here then Nalal, Naa are going to be checked for their types and 'ones' will be dispatced accordingly for each iteration in the loop.
Having looked at stackoverflow post, there are some differences.
I cannot try the codes right now. I wish I could tell something practical perspective using the code actually.
Regards
0.000565 seconds (13 allocations: 1.603 MB)
3.205262 seconds (118.99 k allocations: 15.651 GB, 14.08% gc time)