Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'benchmark'
- # Benchmarks for variations of hash operations.
- #
- # user system total real
- # each_pair 1.670000 0.000000 1.670000 ( 1.685259)
- # each_key 0.540000 0.010000 0.550000 ( 0.540336)
- # each_value 0.530000 0.000000 0.530000 ( 0.538994)
- # keys.each 0.690000 0.000000 0.690000 ( 0.695365)
- # values.each 0.690000 0.000000 0.690000 ( 0.695188)
- #
- Benchmark.bm(20) do |x|
- n = 100000
- hash = {}
- ('a'..'z').to_a.each do |letter|
- hash[letter] = letter.upcase
- end
- x.report "each_pair" do
- n.times { hash.each_pair {|key, value|} }
- end
- x.report "each_key" do
- n.times { hash.each_key {|key|} }
- end
- x.report "each_value" do
- n.times { hash.each_value {|value|} }
- end
- x.report "keys.each" do
- n.times { hash.keys.each {|key|} }
- end
- x.report "values.each" do
- n.times { hash.values.each {|value|} }
- end
- end
Add Comment
Please, Sign In to add comment