In debugging some code I wrote, I noticed that there seems to be a significant difference between x = x .+ 1
and writing the loop out explicitly. In particular, the following code runs in 0.006 seconds and reports only 2 allocations totaling 64KiB.
function main()
N = 5000
M = 2^13
x = zeros(M)
for _ in 1:N
for j in 1:M
x[j] = x[j] + 1
end
#x = x .+ 1
end
end
@time main()
However, if I uncomment the line x = x .+ 1
, the code runs in 0.3 seconds and reports 10k allocations totaling 300 MiB. Moreover, if I increase N
to 20000 the memory usage increases to 1.2GiB – the array does not change size just the number of iterations. Why is the memory usage so large? Should I avoid using broadcasting in loops for this reason? I assume I am making some fundamental mistake.
If it helps, I am using julia version 1.9.3.