When I run the following
struct Test
x :: Array{Float64, 1}
y :: Array{Float64, 1}
end
function test()
var = Test([1, 2], [1, 2])
@time @. var.x = var.y - var.x/var.y^2
@time @. var.x = var.y - var.x/(var.y*var.y)
return nothing
end
test()
I get
0.000001 seconds (2 allocations: 16 bytes)
0.000000 seconds
What’s the difference? Why do I get memory allocation when using var.y^2
(first calculation) but not with (var.y*var.y)
(second calculation)? I’m using Julia 1.6.1, and I think I was not getting this difference in behavior in previous Julia versions (may be 1.5) (although the present code seems pretty useless, I’m actually getting something similar in an important piece of code, and I’m getting annoyed because I don’t understand what’s happening).
Please help.