# BenchmarkTools keep failing in a loop

**URL:** https://discourse.julialang.org/t/benchmarktools-keep-failing-in-a-loop/41261
**Category:** New to Julia
**Created:** [June 12, 2020, 11:43am UTC](https://discourse.julialang.org/t/benchmarktools-keep-failing-in-a-loop/41261 "2020-06-12T11:43:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tirthasheshpatel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tirthasheshpatel/32/15313_2.png) [@tirthasheshpatel](https://discourse.julialang.org/u/tirthasheshpatel)
#### Post date: [June 12, 2020, 11:43am UTC](https://discourse.julialang.org/t/benchmarktools-keep-failing-in-a-loop/41261/1 "2020-06-12T11:43:41Z")

</div>

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
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!

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [June 12, 2020, 11:50am UTC](https://discourse.julialang.org/t/benchmarktools-keep-failing-in-a-loop/41261/2 "2020-06-12T11:50:54Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![tirthasheshpatel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tirthasheshpatel/32/15313_2.png) [@tirthasheshpatel](https://discourse.julialang.org/u/tirthasheshpatel)
#### Post date: [June 12, 2020, 11:52am UTC](https://discourse.julialang.org/t/benchmarktools-keep-failing-in-a-loop/41261/3 "2020-06-12T11:52:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [June 12, 2020, 11:54am UTC](https://discourse.julialang.org/t/benchmarktools-keep-failing-in-a-loop/41261/4 "2020-06-12T11:54:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![tirthasheshpatel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tirthasheshpatel/32/15313_2.png) [@tirthasheshpatel](https://discourse.julialang.org/u/tirthasheshpatel)
#### Post date: [June 12, 2020, 11:59am UTC](https://discourse.julialang.org/t/benchmarktools-keep-failing-in-a-loop/41261/5 "2020-06-12T11:59:05Z")

</div>

Thanks for that too! 🙂
