Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'rubygems'
require 'active_support'
require "rbench"
class Hash
def fast_reverse_merge( other_hash )
self.merge( other_hash ){|k,o,n| o }
end
def fast_reverse_merge!( other_hash )
self.merge!( other_hash ){|k,o,n| o }
end
end
first = { :one => 1, :two => 2 }
second = { :three => 3, :four => 4 }
RBench.run(10_000) do
report "Hash#reverse_merge!" do
first.reverse_merge!( second )
end
report "Hash#fast_reverse_merge!" do
first.fast_reverse_merge!( second )
end
report "Hash#reverse_merge" do
first.reverse_merge( second )
end
report "Hash#fast_reverse_merge" do
first.fast_reverse_merge( second )
end
end
=begin
Results |
------------------------------------------
Hash#reverse_merge! 0.035 |
Hash#fast_reverse_merge! 0.018 |
Hash#reverse_merge 0.020 |
Hash#fast_reverse_merge 0.031 |
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment