-
-
Save mks-m/3122511 to your computer and use it in GitHub Desktop.
ljust speed test
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
# 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