Skip to content

Instantly share code, notes, and snippets.

@peterc
Created June 8, 2011 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterc/1014347 to your computer and use it in GitHub Desktop.
Save peterc/1014347 to your computer and use it in GitHub Desktop.
COUNT = 10_000_000
a = [1, 2, 3, 4]
h = { :x => 10, :y => 20, :z => 30 }
require 'benchmark'
Benchmark.bm(20) do |b|
b.report("count arr:") { COUNT.times { a.count } }
b.report("size arr:") { COUNT.times { a.size } }
b.report("count hash:") { COUNT.times { h.count } }
b.report("size hash:") { COUNT.times { h.size } }
end
# user system total real
#count arr: 1.320000 0.000000 1.320000 ( 1.312148)
#size arr: 0.920000 0.000000 0.920000 ( 0.920246)
#count hash: 6.380000 0.010000 6.390000 ( 6.383174)
#size hash: 0.910000 0.000000 0.910000 ( 0.909669)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment