on master help sign in
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%.

commit31c7eb61f31d5e9962f3f10d248a46a9bb1b6bb9 authorBryan O'Sullivan <bos@serpentine.com> dateTue 19 Mar 2013, 21:12 (14 years ago) parent4569ab6
1 file changed+12−2
Data/Text/Lazy/Builder/Int.hs+12−2
Data/Text/Lazy/Builder/Int.hs+12−2
@@ -125,8 +125,18 @@ countDigits v0 = go 1 (fromIntegral v0 :: Word64)
125125 | v < 10 = k
126126 | v < 100 = k + 1
127127 | v < 1000 = k + 2
128− | v < 10000 = k + 3
129− | otherwise = go (k+4) (v `quot` 10000)
128+ | v < 1000000000000 =
129+ k + if v < 100000000
130+ then if v < 1000000
131+ then if v < 10000
132+ then 3
133+ else 4 + fin v 100000
134+ else 6 + fin v 10000000
135+ else if v < 10000000000
136+ then 8 + fin v 1000000000
137+ else 10 + fin v 100000000000
138+ | otherwise = go (k + 12) (v `quot` 1000000000000)
139+ fin v n = if v >= n then 1 else 0
130140
131141hexadecimal :: Integral a => a -> Builder
132142{-# SPECIALIZE hexadecimal :: Int -> Builder #-}