Skip to content

Instantly share code, notes, and snippets.

@timhaines
Created June 24, 2009 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timhaines/134944 to your computer and use it in GitHub Desktop.
Save timhaines/134944 to your computer and use it in GitHub Desktop.
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