@@ -1,11 +1,10 @@
1−-- | Replace a string by another string in a file
1+-- | Replace a string by another string
22--
33module Data.Text.Benchmarks.Replace
44 ( benchmark
55 ) where
66
7−import Criterion (Benchmark, bgroup, bench)
8−import System.IO (Handle)
7+import Criterion (Benchmark, bgroup, bench, nf)
98import qualified Data.ByteString.Char8 as B
109import qualified Data.ByteString.Lazy as BL
1110import qualified Data.ByteString.Lazy.Search as BL
@@ -13,19 +12,14 @@ import qualified Data.Text.Lazy as TL
1312import qualified Data.Text.Lazy.Encoding as TL
1413import qualified Data.Text.Lazy.IO as TL
1514
16−benchmark :: FilePath -> Handle -> String -> String -> IO Benchmark
17−benchmark fp sink pat sub = return $ bgroup "Replace"
18− -- We have benchmarks for lazy text and lazy bytestrings. We also benchmark
19− -- without the acual replacement, so we can get an idea of what time is
20− -- spent on IO and computations.
21− [ bench "LazyText" $ TL.readFile fp >>=
22− TL.hPutStr sink . TL.replace tpat tsub
23− , bench "LazyTextNull" $ TL.readFile fp >>= TL.hPutStr sink
24−
25− , bench "LazyByteString" $ BL.readFile fp >>=
26− BL.hPutStr sink . BL.replace bpat bsub
27− , bench "LazyByteStringNull" $ BL.readFile fp >>= BL.hPutStr sink
28− ]
15+benchmark :: FilePath -> String -> String -> IO Benchmark
16+benchmark fp pat sub = do
17+ tl <- TL.readFile fp
18+ bl <- BL.readFile fp
19+ return $ bgroup "Replace"
20+ [ bench "LazyText" $ nf (TL.length . TL.replace tpat tsub) tl
21+ , bench "LazyByteString" $ nf (BL.length . BL.replace bpat bsub) bl
22+ ]
2923 where
3024 tpat = TL.pack pat
3125 tsub = TL.pack sub