Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Created July 31, 2013 20:05
Show Gist options
  • Save agmcleod/6125657 to your computer and use it in GitHub Desktop.
Save agmcleod/6125657 to your computer and use it in GitHub Desktop.
array testing
require 'benchmark'
arr = [
]
Benchmark.bm do |x|
x.report 'class check' do
100.times.each do |i|
100_000.times.each do |j|
if arr[i].is_a? Array
arr[i] << j
else
arr[i] = [j]
end
end
end
end
x.report '||=' do
100.times.each do |i|
100_000.times.each do |j|
arr[i] ||= []
arr[i] << j
end
end
end
end
# user system total real
# class check 2.020000 0.040000 2.060000 ( 2.058548)
# ||= 2.000000 0.070000 2.070000 ( 2.177428)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment