Guest User

Untitled

a guest
Apr 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. require 'benchmark'
  2.  
  3. # Benchmarks for variations of hash operations.
  4. #
  5. # user system total real
  6. # each_pair 1.670000 0.000000 1.670000 ( 1.685259)
  7. # each_key 0.540000 0.010000 0.550000 ( 0.540336)
  8. # each_value 0.530000 0.000000 0.530000 ( 0.538994)
  9. # keys.each 0.690000 0.000000 0.690000 ( 0.695365)
  10. # values.each 0.690000 0.000000 0.690000 ( 0.695188)
  11. #
  12. Benchmark.bm(20) do |x|
  13.  
  14. n = 100000
  15. hash = {}
  16. ('a'..'z').to_a.each do |letter|
  17. hash[letter] = letter.upcase
  18. end
  19.  
  20. x.report "each_pair" do
  21. n.times { hash.each_pair {|key, value|} }
  22. end
  23.  
  24. x.report "each_key" do
  25. n.times { hash.each_key {|key|} }
  26. end
  27.  
  28. x.report "each_value" do
  29. n.times { hash.each_value {|value|} }
  30. end
  31.  
  32. x.report "keys.each" do
  33. n.times { hash.keys.each {|key|} }
  34. end
  35.  
  36. x.report "values.each" do
  37. n.times { hash.values.each {|value|} }
  38. end
  39.  
  40. end
Add Comment
Please, Sign In to add comment