Created
March 29, 2013 22:32
-
-
Save postmodern/5274138 to your computer and use it in GitHub Desktop.
Benchmark of Hash#merge vs. Ruby 2.0 keyword arguments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'benchmark' | |
Benchmark.bm do |b| | |
n = 1_000_000 | |
hash1 = {a: 1, b: 2, c: 3} | |
hash2 = {x: 1, y: 2, z: 3} | |
b.report('Hash#merge') do | |
n.times { {a: 1, b: 2, c: 3}.merge(hash2) } | |
end | |
b.report('keyword args') do | |
n.times { {a: 1, b: 2, c: 3, **hash2} } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@postmodern the results are much closer with
Hash#update
:EDIT: apparently I didn't understand the context of the benchmark: https://twitter.com/raggi/status/317903577668534272