Created
September 21, 2011 12:13
-
-
Save sra448/1231890 to your computer and use it in GitHub Desktop.
test performance of flatten vs flatten!
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
def test_preformance(name, *args) | |
start = Time.new | |
100000.times do |i| | |
yield(args) | |
end | |
time = (Time.new - start) * 1000 | |
puts "#{name} time: #{time} ms" | |
return time | |
end | |
def test(how_many) | |
test_array = [1, 2, 3, [ 1, 2, 3, 4, [1, 2, 3, 4, 5, 6, 7], 8, [9]], 10] | |
time_destructive = 0 | |
how_many.times { |i| time_destructive += test_preformance("#{i + 1}, destructive:", test_array) { |foo| foo.flatten! }; } | |
puts "average: #{time_destructive / how_many} ms" | |
puts | |
time_non_destructive = 0 | |
how_many.times { |i| time_non_destructive += test_preformance("#{i + 1}, non_destructive:", test_array) { |foo| bar = foo.flatten }; } | |
puts "average: #{time_non_destructive / how_many} ms" | |
puts | |
end | |
test(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment