@@ -1,11 +1,10 @@
1−-- | Replace a string by another string in a file
1+-- | Replace a string by another string
3module Data.Text.Benchmarks.Replace
3module Data.Text.Benchmarks.Replace
4 ( benchmark
4 ( benchmark
7−import Criterion (Benchmark, bgroup, bench)
7+import Criterion (Benchmark, bgroup, bench, nf)
8−import System.IO (Handle)
9import qualified Data.ByteString.Char8 as B
8import qualified Data.ByteString.Char8 as B
10import qualified Data.ByteString.Lazy as BL
9import qualified Data.ByteString.Lazy as BL
11import qualified Data.ByteString.Lazy.Search as BL
10import qualified Data.ByteString.Lazy.Search as BL
@@ -13,19 +12,14 @@ import qualified Data.Text.Lazy as TL
13import qualified Data.Text.Lazy.Encoding as TL
12import qualified Data.Text.Lazy.Encoding as TL
14import qualified Data.Text.Lazy.IO as TL
13import qualified Data.Text.Lazy.IO as TL
16−benchmark :: FilePath -> Handle -> String -> String -> IO Benchmark
15+benchmark :: FilePath -> String -> String -> IO Benchmark
16+benchmark fp pat sub = do
17−benchmark fp sink pat sub = return $ bgroup "Replace"
19+ 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
20+ [ bench "LazyText" $ nf (TL.length . TL.replace tpat tsub) tl
23− , bench "LazyTextNull" $ TL.readFile fp >>= TL.hPutStr sink
25− , bench "LazyByteString" $ BL.readFile fp >>=
21+ , bench "LazyByteString" $ nf (BL.length . BL.replace bpat bsub) bl
26− BL.hPutStr sink . BL.replace bpat bsub
27− , bench "LazyByteStringNull" $ BL.readFile fp >>= BL.hPutStr sink
30 tpat = TL.pack pat
24 tpat = TL.pack pat
31 tsub = TL.pack sub
25 tsub = TL.pack sub