Skip to content

Instantly share code, notes, and snippets.

@jglass
Created March 23, 2011 16:54
Show Gist options
  • Save jglass/883461 to your computer and use it in GitHub Desktop.
Save jglass/883461 to your computer and use it in GitHub Desktop.
Benchmark of count vs. length in Ruby
require 'rubygems'
require 'benchmark'
array = []
1.upto(1000000) { |i| array << i }
Benchmark.bm(7) do |r|
r.report("Count:") { array.count }
r.report("Length:") { array.length }
end
# user system total real
# Count: 0.000000 0.000000 0.000000 ( 0.000010)
# Length: 0.000000 0.000000 0.000000 ( 0.000006)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment