Skip to content

Instantly share code, notes, and snippets.

@eneagoe
Created December 10, 2013 13:36
Show Gist options
  • Save eneagoe/7890670 to your computer and use it in GitHub Desktop.
Save eneagoe/7890670 to your computer and use it in GitHub Desktop.
benchmark number reverse
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