Using @benchmark in a for loop with a dictionary

While I don’t use LightGraphs, I hope the following more-or-less achieves what you want.

using BenchmarkTools

# try a bunch of matrix multiplications with different type
mydict = Dict{String, Type}(
    "Float16" => Float16,
    "Float32" => Float32,
    "Float64" => Float64,
)

function bench(d::Dict)
    for (key, value) in d
        println("Current type = $key")
        b = @benchmark rand(value, 10, 10) * rand(value, 10, 10)
        display(b)
    end
end

bench(mydict)

Expected output:

julia> bench(mydict)
Current type = Float32
BenchmarkTools.Trial:
  memory estimate:  1.06 KiB
  allocs estimate:  7
  --------------
  minimum time:     23.792 μs (0.00% GC)
  median time:      25.339 μs (0.00% GC)
  mean time:        27.222 μs (0.00% GC)
  maximum time:     110.877 μs (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1
Current type = Float64
BenchmarkTools.Trial:
  memory estimate:  1.06 KiB
  allocs estimate:  7
  --------------
  minimum time:     25.104 μs (0.00% GC)
  median time:      26.907 μs (0.00% GC)
  mean time:        29.837 μs (0.00% GC)
  maximum time:     177.899 μs (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1
Current type = Float16
BenchmarkTools.Trial:
  memory estimate:  1.06 KiB
  allocs estimate:  7
  --------------
  minimum time:     27.320 μs (0.00% GC)
  median time:      32.556 μs (0.00% GC)
  mean time:        34.420 μs (0.00% GC)
  maximum time:     115.760 μs (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1

Is there what you’re hoping for?

2 Likes