Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def measure_time task_description
beginning = Time.now
yield
puts "Time to #{task_description}: #{Time.now - beginning} seconds"
end
def add a, i
a << i
end
5.times do
measure_time("add item via method") do
a = []
1000000.times {|i| add a, i}
# puts "length #{a.length}"
end
end
5.times do
measure_time("add item directly") do
a = []
1000000.times {|i| a << i}
# puts "length #{a.length}"
end
end
5.times do
measure_time("with method") do
a = []
1000000.times {|i| add a, i}
# puts "length #{a.length}"
end
end
5.times do
measure_time("without method") do
a = []
1000000.times {|i| a << i}
# puts "length #{a.length}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.