Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@equivalent
Created July 16, 2012 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save equivalent/3122349 to your computer and use it in GitHub Desktop.
Save equivalent/3122349 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
1.9.3p194 :019 > def shot
1.9.3p194 :020?> a=Time.now
1.9.3p194 :021?> 1000000.times do
1.9.3p194 :022 > "5".ljust(3, "0")
1.9.3p194 :023?> end
1.9.3p194 :024?> b =Time.now
1.9.3p194 :025?> p (b-a)*1000
1.9.3p194 :026?> end
=> nil
1.9.3p194 :027 > shot
=========
473.51715 # winner !
========
=> 473.51715
1.9.3p194 :028 > def shot
1.9.3p194 :029?> a=Time.now
1.9.3p194 :030?> 1000000.times do
1.9.3p194 :031 > ("%03d" % (5.to_s.reverse.to_i)).to_s.reverse
1.9.3p194 :032?> end
1.9.3p194 :033?> b = Time.now
1.9.3p194 :034?> p (b-a) * 1000
1.9.3p194 :035?> end
=> nil
1.9.3p194 :036 > shot
==========
1649.558327
==========
=> 1649.558327
1.9.3p194 :042 > def shot
1.9.3p194 :043?> a=Time.now
1.9.3p194 :044?> 1000000.times do
1.9.3p194 :045 > int * 10**([3 - Math.log10(5).to_i - 1, 0].max)
1.9.3p194 :046?> end
1.9.3p194 :047?> b = Time.now
1.9.3p194 :048?> p (b-a) * 1000
1.9.3p194 :049?> end
=> nil
===========
1244.009602
===========
=> 1244.009602
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment