thank you. I did read the guide. now, some of the rules in the guide make obvious sense to me (don’t use abstract types or type-changers), others are too far off for me to encounter.
the beginning of a new language is always overwhelming, and I am not clear what sins I have committed that have slowed down the code. right now, I need to understand simple rules. why was my seemingly simple and straightforward loop so terribly slow? I need to understand both basic reasons (to generalize) and sensible generalizations that transcend my example and that I can keep in mind for the future. the following are still slow:
@time begin; RNG= MersenneTwister(1234); function bench1(N); [randexp(RNG, Float32) for i in 1:N]; end; bench1(MC); end;
0.603805 seconds (10.01 M allocations: 191.413 MiB, 0.84% gc time)
@time begin; RNG= MersenneTwister(1234); [randexp(RNG, Float32) for i in 1:MC]; end;
0.648537 seconds (10.01 M allocations: 191.369 MiB, 6.71% gc time)
above are slow. below are >5 times faster:
@time begin; function bench1(N); RNG=MersenneTwister(1234); [randexp(RNG, Float32) for i in 1:N]; end; bench1(MC); end;
0.151507 seconds (12.70 k allocations: 38.847 MiB, 27.09% gc time)
@time begin; function bench1(N); RNG=MersenneTwister(1234); for i in 1:N begin randexp(RNG, Float32) end; end; end; bench1(MC); end;
0.083991 seconds (1.19 k allocations: 76.115 KiB)
@time begin; function bench1(N); RNG=MersenneTwister(1234); for i in 1:N; begin randexp(RNG, Float32) end; end; end; bench1(MC); end;
0.086294 seconds (1.19 k allocations: 76.115 KiB)
if I understand this correctly, I infer the following rule: ** julia’s outermost global scope is slow; julia is fast only inside functions **. the reason for this seems unintuitive to me. or am I wrong here?
now, the following function is also very slow,
@time begin; RNG=MersenneTwister(1234); function bench1(N); for i in 1:N; begin randexp(RNG, Float32) end; end; end; bench1(MC); end;
0.509664 seconds (10.00 M allocations: 152.652 MiB, 0.55% gc time)
is the rule then ** inside a function, any reference to an above-scope variable made inside the function slows it down terribly ** ? and ** this should be avoided by …???.. *** . one placeholder could be “passing all above-scope variables instead as arguments into the function.”