Skip to content

Instantly share code, notes, and snippets.

@mks-m
Forked from equivalent/speedtest
Created July 16, 2012 12:50
Show Gist options
  • Save mks-m/3122511 to your computer and use it in GitHub Desktop.
Save mks-m/3122511 to your computer and use it in GitHub Desktop.
ljust speed test
# just speed test for http://stackoverflow.com/questions/11502629/how-to-do-number-to-string-suffix/11502715#11502715
# if we properly test int<->str conversion the log_10 method is about 30% faster
begin
int = 5
a = Time.now.to_f
10_000_000.times { int.to_s.ljust(3, "0").to_i }
puts Time.now.to_f - a # => 1.3s
a = Time.now.to_f
10_000_000.times { int * 10**(3 - Math.log10(int).to_i - 1) }
puts Time.now.to_f - a # => 1s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment