I have difficulties understanding the number of allocations. In the following:
const v1 = [0, 1]
const v2 = [1, 2]
fun1(x) = begin
a = x .+ v1
return a .* v2
end
fun2(x) = @. (x + v1) * v2
julia> @btime fun1([10, 20]);
102.232 ns (3 allocations: 288 bytes)
julia> @btime fun2([10, 20]);
69.386 ns (2 allocations: 192 bytes)
for fun1()
, one allocation for a
+ one allocation for output, so I think there should be 2 allocations, but in fact there’re 3.
for fun2()
, only one allocation is needed for output, but in fact there’re 2 allocations.
where is the extra allocation? thanks