いろいろなところで説明されているように、Juliaでは関数で書かないと速度が出ません。
書き方により差は出ますが、百倍以上の差があります。
using BenchmarkTools
a = 0
N0 = 10^8
@time for x in 1:N0
global a
a += x
end
print(a)
9.758943 seconds (400.00 M allocations: 7.451 GiB, 2.91% gc time) 5000000050000000
a = 0
N0 = 10^8
function f(a, N0)
a = 0
for x in 1:N0
a += x
end
return a
end
@time b = f(a, N0)
print(b)
0.006952 seconds (2.39 k allocations: 168.797 KiB, 99.45% compilation time) 5000000050000000