Skip to content

@peterc /count_vs_size.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.