Hi folks,
I have writen a diffusion Monte Carlo code in Julia 0.5.0 that, unfortunately, performs about 5 or 6 times slower than the corresponding fortran version. I have learned that it is very easy to write bad code in Julia, and that several detailed things should be taken into account in order to make the code fast -but somehow I fail to understand basic things. I hope somebody here can help me understand what happens to this piece of code, to start with.
Consider the following array:
WW = rand(64,3,10);
and the following function
function Drift_Force(Wr::Array{Float64},Lbox::Float64)
Np,dim,NW = size(Wr)
F = zeros(Np,dim,NW)
ri = zeros(dim,1)
rj = zeros(dim,1)
rij = zeros(dim,1)
for iw in 1:NW
for i in 1:Np-1
ri = zeros(3,1);
for j in i+1:Np
rj = (1.0,2.0,3.0);
end;
end;
end;
end;
Now I try
Lbox = 5.75;
@time Drift_Force(WW,Lbox);
to get
0.000081 seconds (638 allocations: 84.516 KB)
But if I replace the rj = (1.0,2.0,3.0) line with rj = zeros(3,1)
I get
0.001496 seconds (20.80 k allocations: 2.236 MB)
instead. This last version takes much longer and surprisingly allocates much more memory.
Would you say this is normal, expected behaviour? How can it be that by adding three Floats in an
array I get an overhead of 3MB? I ask because this is just part of a more complex code that, in the end, keeps allocating and allocating memory to the point that it becomes much slower than the fortran version…
Thanks in advance for your kind help,
Ferran.