# Benchmarking function (using \`@benchmark\`) for various input sizes

**URL:** https://discourse.julialang.org/t/benchmarking-function-using-benchmark-for-various-input-sizes/76306
**Category:** Performance
**Created:** [February 12, 2022, 5:30pm UTC](https://discourse.julialang.org/t/benchmarking-function-using-benchmark-for-various-input-sizes/76306 "2022-02-12T17:30:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [February 12, 2022, 5:30pm UTC](https://discourse.julialang.org/t/benchmarking-function-using-benchmark-for-various-input-sizes/76306/1 "2022-02-12T17:30:46Z")

</div>

I have a function. I want to benchmark it for various inputs (originally I am solving a differential equations over various timespans). Naturally I would do something like:

```julia
using BenchmarkTools
bs = 
for l in [10,100,1000]
    b = @benchmark rand(l).^2
end

```

(probably compressed using `map()`, but this makes the mwe clearer)  
This, however, yields an error:

```julia
UndefVarError: l not defined

Stacktrace:
  [1] var"##core#519"()
    @ Main ~/.julia/packages/BenchmarkTools/7xSXH/src/execution.jl:489
  [2] var"##sample#520"(::Tuple{}, __params::BenchmarkTools.Parameters)
    @ Main ~/.julia/packages/BenchmarkTools/7xSXH/src/execution.jl:495
  [3] _run(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters; verbose::Bool, pad::String, kwargs::Base.Pairs{Symbol, Integer, NTuple{4, Symbol}, NamedTuple{(:samples, :evals, :gctrial, :gcsample), Tuple{Int64, Int64, Bool, Bool}}})
    @ BenchmarkTools ~/.julia/packages/BenchmarkTools/7xSXH/src/execution.jl:99
  [4] #invokelatest#2
    @ ./essentials.jl:718 [inlined]
  [5] #run_result#45
    @ ~/.julia/packages/BenchmarkTools/7xSXH/src/execution.jl:34 [inlined]
  [6] run(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters; progressid::Nothing, nleaves::Float64, ndone::Float64, kwargs::Base.Pairs{Symbol, Integer, NTuple{5, Symbol}, NamedTuple{(:verbose, :samples, :evals, :gctrial, :gcsample), Tuple{Bool, Int64, Int64, Bool, Bool}}})
    @ BenchmarkTools ~/.julia/packages/BenchmarkTools/7xSXH/src/execution.jl:117
  [7] #warmup#54
    @ ~/.julia/packages/BenchmarkTools/7xSXH/src/execution.jl:169 [inlined]
  [8] warmup(item::BenchmarkTools.Benchmark)
    @ BenchmarkTools ~/.julia/packages/BenchmarkTools/7xSXH/src/execution.jl:169
  [9] macro expansion
    @ ~/.julia/packages/BenchmarkTools/7xSXH/src/execution.jl:393 [inlined]
 [10] top-level scope
    @ In[42]:4
 [11] eval
    @ ./boot.jl:373 [inlined]
 [12] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1196

```

Is there a way to get around this, or otherwise accomplish what I want to accomplish?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 12, 2022, 5:35pm UTC](https://discourse.julialang.org/t/benchmarking-function-using-benchmark-for-various-input-sizes/76306/2 "2022-02-12T17:35:57Z")

</div>

> [@Torkel](#):
>
> `bs = `

first of, for loop does not return so this won’t become an array as you’d probably want it to, also you want to use `$`:

```julia
julia> using BenchmarkTools

julia> bs = map([10,100,1000]) do l
           @benchmark rand($l)
       end;

julia> bs
3-element Vector{BenchmarkTools.Trial}:
 71.882 ns
 160.522 ns
 1.100 μs

julia> bs[1]
BenchmarkTools.Trial: 10000 samples with 974 evaluations.
 Range (min … max): 71.882 ns … 644.366 ns ┊ GC (min … max): 0.00% … 85.38%
 Time (median): 74.186 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 76.662 ns ± 25.452 ns ┊ GC (mean ± σ): 1.72% ± 4.54%

   ▃▆██▇▄▁▂▂▁ ▁▁ ▂
  ▇███████████████▇▆▇▇▆▃▄▁▃▄▄▃▄▃▃▁▁▁▁▃▁▁▁▁▁▁▃▄▅▆▇▇██▇▆▅▆▅▆▇███ █
  71.9 ns Histogram: log(frequency) by time 104 ns <

 Memory estimate: 144 bytes, allocs estimate: 1.

```

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [February 12, 2022, 5:43pm UTC](https://discourse.julialang.org/t/benchmarking-function-using-benchmark-for-various-input-sizes/76306/3 "2022-02-12T17:43:14Z")

</div>

Thanks! Yes, I messed up the example a bit. Anyway, the `$` solves it! Lots of thanks!
