Commits

Bryan O'Sullivan committed faf4377

Replace countDigits with a faster, more complex version

This is taken from Andrei's "Three Optimization Tips for C++" post:
https://www.facebook.com/notes/facebook-engineering/three-optimization-tips-for-c/10151361643253920

It improves performance by up to 15%.

  • Participants
  • Parent commits ef3ffe3

Comments (0)

Files changed (1)

File Data/Text/Lazy/Builder/Int.hs

            | v < 10    = k
            | v < 100   = k + 1
            | v < 1000  = k + 2
-           | v < 10000 = k + 3
-           | otherwise = go (k+4) (v `quot` 10000)
+           | v < 1000000000000 =
+               k + if v < 100000000
+                   then if v < 1000000
+                        then if v < 10000
+                             then 3
+                             else 4 + fin v 100000
+                        else 6 + fin v 10000000
+                   else if v < 10000000000
+                        then 8 + fin v 1000000000
+                        else 10 + fin v 100000000000
+           | otherwise = go (k + 12) (v `quot` 1000000000000)
+        fin v n = if v >= n then 1 else 0
 
 hexadecimal :: Integral a => a -> Builder
 {-# SPECIALIZE hexadecimal :: Int -> Builder #-}