Skip to content

Why it is not possible to use for loops inside the setup block? #392

@bvdmitri

Description

@bvdmitri
julia> using BenchmarkTools

julia> b = @benchmarkable sum(a) setup = begin
           a = zeros(10)
           for i in 1:10
               a[i] = i
           end
       end
Benchmark(evals=1, seconds=5.0, samples=10000)

julia> run(b)
ERROR: UndefVarError: `i` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
  [1] var"##sample#231"(::Tuple{}, __params::BenchmarkTools.Parameters)
    @ Main ~/.julia/packages/BenchmarkTools/1i1mY/src/execution.jl:607
  [2] _run(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters; verbose::Bool, pad::String, warmup::Bool, kwargs::@Kwargs{})
    @ BenchmarkTools ~/.julia/packages/BenchmarkTools/1i1mY/src/execution.jl:113
  [3] _run(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters)
    @ BenchmarkTools ~/.julia/packages/BenchmarkTools/1i1mY/src/execution.jl:109
  [4] #invokelatest#2
    @ ./essentials.jl:1055 [inlined]
  [5] invokelatest
    @ ./essentials.jl:1052 [inlined]
  [6] #run_result#45
    @ ~/.julia/packages/BenchmarkTools/1i1mY/src/execution.jl:48 [inlined]
  [7] run_result
    @ ~/.julia/packages/BenchmarkTools/1i1mY/src/execution.jl:47 [inlined]
  [8] run(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters; progressid::Nothing, nleaves::Float64, ndone::Float64, kwargs::@Kwargs{})
    @ BenchmarkTools ~/.julia/packages/BenchmarkTools/1i1mY/src/execution.jl:143
  [9] run
    @ ~/.julia/packages/BenchmarkTools/1i1mY/src/execution.jl:135 [inlined]
 [10] run(b::BenchmarkTools.Benchmark)
    @ BenchmarkTools ~/.julia/packages/BenchmarkTools/1i1mY/src/execution.jl:135
 [11] top-level scope
    @ REPL[4]:1

foreach works just fine

julia> b = @benchmarkable sum(a) setup=begin
           a = zeros(10)
           foreach(1:10) do i
               a[i] = i
           end
       end
Benchmark(evals=1, seconds=5.0, samples=10000)

julia> run(b)
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
 Range (min  max):   0.001 ns  292.000 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):      0.001 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   10.709 ns ±  18.605 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  █
  █▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄▅ ▂
  0.001 ns        Histogram: frequency by time           42 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions