on master help sign in
Added benchmark for builder.

The benchmark shows that using the locally defined inlineInterleaveST is 13-44% faster than using unsafeInterleaveST. --HG-- extra : convert_revision : 0728b27e0dd2268150fa3816b66a27c04b528bb0

commit39b0a4bbf4d184473b90b1c01aa4a99346d630f9 authorJohan Tibell <johan.tibell@gmail.com> dateTue 27 Apr 2010, 08:43 (16 years ago) parent85c5b89
2 files changed+27−6
Data/Text/Builder.hs+5−5 tests/Benchmarks.hs+22−1
Data/Text/Builder.hs+5−5
@@ -229,9 +229,9 @@ unsafeCopy src sidx dest didx count =
229229 assert (sidx + count <= A.length src) .
230230 assert (didx + count <= A.length dest) $
231231 copy_loop sidx didx 0
232− where
233− copy_loop !i !j !c
234− | c >= count = return ()
235− | otherwise = do A.unsafeWrite dest j (A.unsafeIndex src i)
236− copy_loop (i+1) (j+1) (c+1)
232+ where
233+ copy_loop !i !j !c
234+ | c >= count = return ()
235+ | otherwise = do A.unsafeWrite dest j (A.unsafeIndex src i)
236+ copy_loop (i+1) (j+1) (c+1)
237237{-# INLINE unsafeCopy #-}
tests/Benchmarks.hs+22−1
@@ -1,4 +1,4 @@
1−{-# LANGUAGE GADTs, MagicHash #-}
1+{-# LANGUAGE BangPatterns, GADTs, MagicHash #-}
22
33import qualified Data.ByteString.Char8 as BS
44import qualified Data.ByteString.Lazy.Char8 as BL
@@ -7,8 +7,10 @@ import Control.Monad.Trans (liftIO)
77import Control.Exception (evaluate)
88import Criterion.Main
99import Data.Char
10+import Data.Monoid (mappend, mempty)
1011import qualified Codec.Binary.UTF8.Generic as UTF8
1112import qualified Data.Text as TS
13+import qualified Data.Text.Builder as TB
1214import qualified Data.Text.IO as TS
1315import qualified Data.Text.Lazy as TL
1416import qualified Data.Text.Lazy.IO as TL
@@ -388,6 +390,10 @@ main = do
388390 , bench "bl" $ nf (L.length . BL.zipWith min blb) bla
389391 , bench "l" $ nf (L.length . L.zipWith min lb) la
390392 ]
393+ ],
394+ bgroup "builder" [
395+ bench "mappend char" $ nf (TL.length . TB.toLazyText . mappendNChar 'a') 10000,
396+ bench "mappend text" $ nf (TL.length . TB.toLazyText . mappendNText short) 10000
391397 ]
392398 ]
393399 where
@@ -402,6 +408,7 @@ main = do
402408 f (C# c#) = C# (chr# (ord# c# +# 1#))
403409 len l _ = l + (1::Int)
404410 replicat n = concat . L.replicate n
411+ short = TS.pack "short"
405412
406413chunksOf :: Int -> BS.ByteString -> [BS.ByteString]
407414chunksOf k = go
@@ -409,3 +416,17 @@ chunksOf k = go
409416 go t = case BS.splitAt k t of
410417 (a,b) | BS.null a -> []
411418 | otherwise -> a : go b
419+
420+mappendNChar :: Char -> Int -> TB.Builder
421+mappendNChar c n = go 0 mempty
422+ where
423+ go i !acc
424+ | i < n = go (i+1) (acc `mappend` TB.singleton c)
425+ | otherwise = acc
426+
427+mappendNText :: TS.Text -> Int -> TB.Builder
428+mappendNText t n = go 0 mempty
429+ where
430+ go i !acc
431+ | i < n = go (i+1) (acc `mappend` TB.fromText t)
432+ | otherwise = acc