# Using @benchmark in a for loop with a dictionary

**URL:** <https://discourse.julialang.org/t/using-benchmark-in-a-for-loop-with-a-dictionary/53780>\
**Category:** General Usage\
**Tags:** dictionary, benchmark, benchmarktools, loops\
**Created:** [January 22, 2021, 1:32pm UTC](https://discourse.julialang.org/t/using-benchmark-in-a-for-loop-with-a-dictionary/53780 "2021-01-22T13:32:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aje12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aje12/32/18950_2.png) [@Aje12](https://discourse.julialang.org/u/Aje12)\
**Post date:** [January 22, 2021, 1:32pm UTC](https://discourse.julialang.org/t/using-benchmark-in-a-for-loop-with-a-dictionary/53780/1 "2021-01-22T13:32:52Z")

</div>

Hi all,

I am pretty new to Julia and I am trying to use the @benchmark macro to benchmark the PageRank algorithm on multiple graphs which are stored in a dictionary. The aim is to store the graphs in the dictionary then loop through it, printing the benchmarks for each. My code so far is below:

```julia
#Using SNAPDatasets.jl package to load different graphs into a dictionary
SNAPGRAPHS = Dict{String, LightGraphs.SimpleGraphs.AbstractSimpleGraph{Int64}}(
    "twitter_u" => loadsnap(:ego_twitter_u),
    "twitter_d" => loadsnap(:ego_twitter_d),
    "caida" => loadsnap(:as_caida),
    "facebook" => loadsnap(:facebook_combined),
    "patent" => loadsnap(:patent_cit_us)
)

for (key, value) in SNAPGRAPHS
    #print name (key) and benchmarks for each value (graph) in the pagerank algorithm
   #@benchmark name pagerank(value)
end
 

```

The for loop is pseudo code atm, I have tried different things but none seem to work so far. Is it possible to achieve what I want to do or is it wishful thinking? Any help/tips would be much appreciated 🙂 .

---

<div class="post-metadata">

**Author:** ![biona001](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/biona001/32/16497_2.png) [@biona001](https://discourse.julialang.org/u/biona001)\
**Post date:** [January 23, 2021, 1:51am UTC](https://discourse.julialang.org/t/using-benchmark-in-a-for-loop-with-a-dictionary/53780/2 "2021-01-23T01:51:18Z")

</div>

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

```julia
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
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?

---

<div class="post-metadata">

**Author:** ![Aje12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aje12/32/18950_2.png) [@Aje12](https://discourse.julialang.org/u/Aje12)\
**Post date:** [January 23, 2021, 9:05pm UTC](https://discourse.julialang.org/t/using-benchmark-in-a-for-loop-with-a-dictionary/53780/3 "2021-01-23T21:05:01Z")

</div>

Thank you very much for this! Helped massively 🙂
