Skip to content

Instantly share code, notes, and snippets.

@Arkoniak
Created January 11, 2017 21:16
Show Gist options
  • Save Arkoniak/ba09fbe189d6818d3edd3d89de63f5d7 to your computer and use it in GitHub Desktop.
Save Arkoniak/ba09fbe189d6818d3edd3d89de63f5d7 to your computer and use it in GitHub Desktop.
Compare performance of list comprehension and map conversion
using BenchmarkTools
# Prepare random dictionary
d = Dict{Symbol, Real}()
for _ in 1:1000
d[Symbol(randstring())] = rand()
end
function test_dict()
Dict(k => v for (k, v) in d)
end
function test_map()
Dict(map((x) -> x[1] => x[2], d))
end
println(@benchmark test_dict())
println(@benchmark test_map())
# Results test_dict
# BenchmarkTools.Trial:
# memory estimate: 174.34 kb
# allocs estimate: 3072
# --------------
# minimum time: 14.802 ms (0.00% GC)
# median time: 16.320 ms (0.00% GC)
# mean time: 16.534 ms (0.31% GC)
# maximum time: 24.133 ms (30.02% GC)
# --------------
# samples: 303
# evals/sample: 1
# time tolerance: 5.00%
# memory tolerance: 1.00%
# Results test_map
# BenchmarkTools.Trial:
# memory estimate: 240.81 kb
# allocs estimate: 3057
# --------------
# minimum time: 15.111 ms (0.00% GC)
# median time: 17.779 ms (0.00% GC)
# mean time: 18.827 ms (0.29% GC)
# maximum time: 30.831 ms (0.00% GC)
# --------------
# samples: 266
# evals/sample: 1
# time tolerance: 5.00%
# memory tolerance: 1.00%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment