Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
require 'benchmark'
require 'benchmark/ips'
seed = [1,2,3]
map_proc = ->(i){ [i,i] }
Benchmark.ips do |x|'inject') { seed.inject({}){ |mem, i| mem[i]=i; mem } }'to_h') { }'Hash[map]') { Hash[] }'Hash[*flat_map]') { Hash[*seed.flat_map(&map_proc)] }'lazy.to_h') { }
Warming up --------------------------------------
inject 74.295k i/100ms
to_h 78.415k i/100ms
Hash[map] 73.396k i/100ms
Hash[*flat_map] 55.862k i/100ms
lazy.to_h 14.441k i/100ms
Calculating -------------------------------------
inject 946.430k (± 3.0%) i/s - 4.755M in 5.029162s
to_h 970.956k (± 3.5%) i/s - 4.862M in 5.013636s
Hash[map] 894.628k (± 4.6%) i/s - 4.477M in 5.016057s
Hash[*flat_map] 644.230k (± 3.9%) i/s - 3.240M in 5.037430s
lazy.to_h 149.488k (± 2.2%) i/s - 750.932k in 5.025786s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.