Created
September 2, 2009 14:42
-
-
Save jodosha/179741 to your computer and use it in GitHub Desktop.
This file contains 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
require "benchmark" | |
TIMES = 5_000_000 | |
HASH = { "foo" => "1000", "bar" => "2000" } | |
Benchmark.bm(30) do |b| | |
b.report("old implementation") do | |
TIMES.times do |i| | |
keys = %w(foo bar) | |
result = HASH.inject({}) do |hash, value| | |
key = keys.shift | |
value.nil? ? hash : hash.merge(key => value) | |
end | |
end | |
end | |
b.report("new implementation") do | |
TIMES.times do |i| | |
result = {} | |
keys = %w(foo bar) | |
HASH.each do |value| | |
key = keys.shift | |
result.merge!(key => value) unless value.nil? | |
end | |
end | |
end | |
end | |
__END__ | |
user system total real | |
old implementation 43.760000 0.150000 43.910000 ( 44.619332) | |
new implementation 27.990000 0.080000 28.070000 ( 28.225878) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment