Hello,
I have a simple toy example in which I compare two methods of creating persistent variables. In my case, both methods exhibit identical performance.
Are there any differences to consider regarding safety, compile times, or performance?
The code:
using BenchmarkTools
macro persistent(init)
var = gensym()
eval(:(const $var = $init))
quote
$var
end
end
function exp_function(n::Int) #expensive function to call
rng = MersenneTwister(n)
A = zeros(n,n)
for i in 1:n,j in 1:n
A[i,j] = sum(rand(rng,i,j).-0.5)
end
return A
end
function foo1() #version 1
A = @persistent exp_function(50)
return sum(A)
end
@generated function foo2() # version2
A = exp_function(50)
return :(sum($A))
end