Created
December 10, 2013 13:36
-
-
Save eneagoe/7890670 to your computer and use it in GitHub Desktop.
benchmark number reverse
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' | |
n, exec_time = 123456789123456789123456789123456789123456789, 0 | |
100000.times do | |
exec_time += Benchmark.measure do | |
r = 0 | |
r, n = (r*10 + n % 10), n / 10 while n != 0 | |
r | |
end.real | |
end | |
puts exec_time/100000.0 | |
n, exec_time = "123456789123456789123456789123456789123456789", 0 | |
100000.times do | |
exec_time += Benchmark.measure do | |
n = n.to_s.reverse.to_i | |
end.real | |
end | |
puts exec_time/100000.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment