Created
July 31, 2013 20:05
-
-
Save agmcleod/6125657 to your computer and use it in GitHub Desktop.
array testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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