Skip to content

Instantly share code, notes, and snippets.

@rspec
Created February 24, 2009 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rspec/69296 to your computer and use it in GitHub Desktop.
Save rspec/69296 to your computer and use it in GitHub Desktop.
require 'benchmark'
n = 1_000_000
h = {:a => {'a' => 'A'}, :b => {'b' => 'B'}}
puts %Q{
h.each {|kv| kv[1]}
}
Benchmark.benchmark do |bm|
4.times do
bm.report do
n.times do
h.each {|kv| kv[1]}
end
end
end
end
puts %Q{
h.values.each {|kv| kv}
}
Benchmark.benchmark do |bm|
4.times do
bm.report do
n.times do
h.values.each {|kv| kv}
end
end
end
end
h.each {|kv| kv[1]}
1.390000 0.010000 1.400000 ( 1.431899)
1.390000 0.010000 1.400000 ( 1.420193)
1.380000 0.000000 1.380000 ( 1.393531)
1.380000 0.000000 1.380000 ( 1.422122)
h.values.each {|kv| kv}
1.150000 0.010000 1.160000 ( 1.154314)
1.140000 0.000000 1.140000 ( 1.160401)
1.150000 0.000000 1.150000 ( 1.157688)
1.140000 0.000000 1.140000 ( 1.158936)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment