Using @benchmark in a for loop with a dictionary

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:

#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 :slight_smile: .

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

Thank you very much for this! Helped massively :slight_smile: