Skip to content

Instantly share code, notes, and snippets.

@weapp
Created July 6, 2016 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weapp/0066ca6007fd8654fcb35f9d7dffd98a to your computer and use it in GitHub Desktop.
Save weapp/0066ca6007fd8654fcb35f9d7dffd98a to your computer and use it in GitHub Desktop.
require 'securerandom'
require 'benchmark/ips'
hashs = 20.times.map do
20.times.map { [SecureRandom.hex(2), SecureRandom.hex(2)] }.to_h
end
Benchmark.ips do |x|
x.time = 5
x.warmup = 2
x.report("merge") { hashs.reduce(&:merge) }
x.report("merge!") { hashs.reduce({}, &:merge!) }
x.compare!
end
# Warming up --------------------------------------
# merge 52.000 i/100ms
# merge! 896.000 i/100ms
# Calculating -------------------------------------
# merge 629.419 (±24.8%) i/s - 2.860k in 5.040327s
# merge! 9.915k (±29.9%) i/s - 43.904k in 5.069974s
# Comparison:
# merge!: 9915.1 i/s
# merge: 629.4 i/s - 15.75x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment