BenchmarkTools keep failing in a loop

I am trying to use BenchmarkTools to benchmark some of my algorithms but I keep getting an error I don’t really understand. Why does it keep failing in a loop? I think the @benchmark macro isn’t a blocking line due to which len is going out of scope. I am not sure but is this expected?

julia> using BenchmarkTools

julia> for len ∈ 1:10:1000
       @benchmark sort!(a) setup=(a=randn(len));
       end
ERROR: UndefVarError: len not defined
Stacktrace:
 [1] ##sample#256(::BenchmarkTools.Parameters) at C:\Users\tirth\.julia\packages\BenchmarkTools\eCEpo\src\execution.jl:373
 [2] _run(::BenchmarkTools.Benchmark{Symbol("##benchmark#254")}, ::BenchmarkTools.Parameters; verbose::Bool, pad::String, kwargs::Base.Iterators.Pairs{Symbol,Integer,NTuple{4,Symbol},NamedTuple{(:samples, :evals, :gctrial, :gcsample),Tuple{Int64,Int64,Bool,Bool}}}) at C:\Users\tirth\.julia\packages\BenchmarkTools\eCEpo\src\execution.jl:405
 [3] (::Base.var"#inner#2"{Base.Iterators.Pairs{Symbol,Integer,NTuple{5,Symbol},NamedTuple{(:verbose, :samples, :evals, :gctrial, :gcsample),Tuple{Bool,Int64,Int64,Bool,Bool}}},typeof(BenchmarkTools._run),Tuple{BenchmarkTools.Benchmark{Symbol("##benchmark#254")},BenchmarkTools.Parameters}})() at .\essentials.jl:715
 [4] #invokelatest#1 at .\essentials.jl:716 [inlined]
 [5] #run_result#37 at C:\Users\tirth\.julia\packages\BenchmarkTools\eCEpo\src\execution.jl:32 [inlined]
 [6] run(::BenchmarkTools.Benchmark{Symbol("##benchmark#254")}, ::BenchmarkTools.Parameters; progressid::Nothing, nleaves::Float64, ndone::Float64, kwargs::Base.Iterators.Pairs{Symbol,Integer,NTuple{5,Symbol},NamedTuple{(:verbose, :samples, :evals, :gctrial, :gcsample),Tuple{Bool,Int64,Int64,Bool,Bool}}}) at C:\Users\tirth\.julia\packages\BenchmarkTools\eCEpo\src\execution.jl:94
 [7] #warmup#45 at C:\Users\tirth\.julia\packages\BenchmarkTools\eCEpo\src\execution.jl:141 [inlined]
 [8] warmup(::BenchmarkTools.Benchmark{Symbol("##benchmark#254")}) at C:\Users\tirth\.julia\packages\BenchmarkTools\eCEpo\src\execution.jl:141
 [9] top-level scope at C:\Users\tirth\.julia\packages\BenchmarkTools\eCEpo\src\execution.jl:287
 [10] top-level scope at REPL[2]:2

I didn’t see many docs but a quick solution will be appreciated!

for len ∈ 1:10:1000
    @benchmark sort!(a) setup=(a=randn($len));
end
2 Likes

Oh, I was not aware of $ syntax. Thank you so much @Skoffer

Just in case, relevant part of the documentation: https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#interpolating-values-into-benchmark-expressions

2 Likes

Thanks for that too! :slightly_smiling_face: