I have a function that I would like to benchmark. To that end, I would like to time the function for several different argument values, and plot how the runtime changes with the argument values. I can do this using the @elapsed
macro, but to get a more accurate/reliable result I would like to use the @belapsed
macro from BenchmarkTools
. E.g.
julia> using BenchmarkTools
julia> function f()
a = "Hello World"
t = @belapsed println(a)
end
f (generic function with 1 method)
julia> f()
ERROR: UndefVarError: `a` not defined
I cannot use @belapsed
because the function I am benchmarking uses variables in the local scope of the function it is called within. According to the BenchmarkTools
documentation: " @benchmark
is evaluated in global scope, even if called from local scope." I guess the same is true of @belapsed
.
This is a surprising design choice to me, but I don’t see a workaround besides executing the body of the outer function in the global scope, which would be very inconvenient.
Does anyone know a workaround or another package to use so I can get a reliable estimate of the runtime for a function, even when called inside the scope of another function?