public
Last active

  • Download Gist
count_vs_size.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.