Commits

Bryan O'Sullivan committed 96473cc

Add support for strict Text

Comments (0)

Files changed (1)

benchmarks/haskell/Benchmarks/Replace.hs

+{-# LANGUAGE BangPatterns #-}
 -- | Replace a string by another string
 --
 -- Tested in this benchmark:
 import qualified Data.ByteString.Char8 as B
 import qualified Data.ByteString.Lazy as BL
 import qualified Data.ByteString.Lazy.Search as BL
+import qualified Data.ByteString.Search as B
+import qualified Data.Text as T
+import qualified Data.Text.Encoding as T
 import qualified Data.Text.Lazy as TL
 import qualified Data.Text.Lazy.Encoding as TL
 import qualified Data.Text.Lazy.IO as TL
 benchmark fp pat sub = do
     tl <- TL.readFile fp
     bl <- BL.readFile fp
-    return $ bgroup "Replace"
-        [ bench "LazyText"       $ nf (TL.length . TL.replace tpat tsub) tl
-        , bench "LazyByteString" $ nf (BL.length . BL.replace bpat bsub) bl
+    let !t = TL.toStrict tl
+        !b = T.encodeUtf8 t
+    return $ bgroup "Replace" [
+          bench "Text"           $ nf (T.length . T.replace tpat tsub) t
+        , bench "ByteString"     $ nf (BL.length . B.replace bpat bsub) b
+        , bench "LazyText"       $ nf (TL.length . TL.replace tlpat tlsub) tl
+        , bench "LazyByteString" $ nf (BL.length . BL.replace blpat blsub) bl
         ]
   where
-    tpat = TL.pack pat
-    tsub = TL.pack sub
-    bpat = B.concat $ BL.toChunks $ TL.encodeUtf8 tpat
-    bsub = B.concat $ BL.toChunks $ TL.encodeUtf8 tsub
+    tpat  = T.pack pat
+    tsub  = T.pack sub
+    tlpat = TL.pack pat
+    tlsub = TL.pack sub
+    bpat = T.encodeUtf8 tpat
+    bsub = T.encodeUtf8 tsub
+    blpat = B.concat $ BL.toChunks $ TL.encodeUtf8 tlpat
+    blsub = B.concat $ BL.toChunks $ TL.encodeUtf8 tlsub