Why does this broadcasting allocate?
julia> a = rand(241); b = rand(241);
julia> @time a .-= b;
0.111315 seconds (188.85 k allocations: 11.037 MiB, 99.59% compilation time)
julia> @time a .-= b;
0.000016 seconds (2 allocations: 64 bytes)
I know it’s only 64 bytes, but in my actual code, the broadcast is inside a loop… For comparison, if I do the same operation using a loop there is no allocation at all
julia> a = rand(241); b = rand(241);
julia> function minuseq!(a,b)
for i in eachindex(a)
a[i] -= b[i]
end
end
minuseq! (generic function with 1 method)
julia> @time minuseq!(a,b);
0.016635 seconds (4.13 k allocations: 229.832 KiB, 99.81% compilation time)
julia> @time minuseq!(a,b);
0.000005 seconds