Is there a way to avoid the intermediate allocation for a sumproduct scenario?
In the following case I can redo it with a for loop which improves the memory allocation but causes it to take longer to execute (the difference seems small here but I have run it a few times and it seems robust).
vec_1 = rand(100)
vec_2 = rand(100)
function sumproduct_one(x,y)
return sum(x .* y)
end
function sumproduct_two(x,y)
res = 0.0
for i in eachindex(x)
res += x[i] * y[i]
end
return res
end
@btime sumproduct_one(vec_1,vec_2)
# 94.501 ns (2 allocations: 912 bytes)
@btime sumproduct_two(vec_1,vec_2)
# 100.227 ns (1 allocation: 16 bytes)