Skip to content

Instantly share code, notes, and snippets.

@gdiggs gdiggs/results
Created Feb 28, 2015

What would you like to do?
Testing Hash[] instead of #dup
require 'benchmark/ips'
def quux(hash_arg)
hash_arg = hash_arg.dup
10.times { |i| hash_arg.merge!({ "num_#{i}" => i }) }
def corge(hash_arg)
hash_arg = Hash[hash_arg]
10.times { |i| hash_arg.merge!({ "num_#{i}" => i }) }
Benchmark.ips do |x|"merge! with dup") { quux({}) }"merge! with Hash[]") { corge({}) }!
Calculating -------------------------------------
merge! with dup 4.759k i/100ms
merge! with Hash[] 4.863k i/100ms
merge! with dup 52.455k (± 3.7%) i/s - 266.504k
merge! with Hash[] 53.576k (± 3.7%) i/s - 267.465k
merge! with Hash[]: 53575.8 i/s
merge! with dup: 52454.7 i/s - 1.02x slower
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.