I would like to include a function as part of a Julia object (struct
). However, when I, for example, execute
using BenchmarkTools
func(x) = 2*x^2
struct Test
k :: Float64
V :: Array{Float64, 1}
fn :: Function
end
T = Test(0.5, collect(1.0:10.0), func)
@btime @. $T.V = $T.k*$T.fn($T.V)
T = Test(0.5, collect(1.0:10.0), func)
@btime @. $T.V = $T.k*$func($T.V)
I get quite a lot of allocated memory when the function from the object’s field is used in the calculations (1st calculation):
181.873 ns (5 allocations: 112 bytes)
9.416 ns (0 allocations: 0 bytes)
Is there any way to solve this problem? (I am using v1.7.0-beta3
because of the new Mac M1, but I It shouldn’t make any difference, I guess.)