Skip to content

Instantly share code, notes, and snippets.

@bankair
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save bankair/0dbfe9279d93451eed45 to your computer and use it in GitHub Desktop.

Select an option

Save bankair/0dbfe9279d93451eed45 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'benchmark/ips'
Benchmark.ips do |r|
r.report('merge!') do
(1..100).inject({}) do |h, e|
h.merge!(e => e)
end
end
ref = (1..100).to_a
r.report('Hash[]') do
Hash[ref.map{|e|[e, e]}]
end
r.report('[]=') do
(1..100).inject({}) do |h, e|
h[e] = e
h
end
end
end
# Calculating -------------------------------------
# merge! 1172 i/100ms
# Hash[] 2363 i/100ms
# []= 2790 i/100ms
# -------------------------------------------------
# merge! 12282.7 (±6.1%) i/s - 62116 in 5.075818s
# Hash[] 25815.9 (±3.0%) i/s - 129965 in 5.038789s
# []= 29528.6 (±5.6%) i/s - 147870 in 5.021272s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment