on master help sign in
Purify Replace benchmark
60ab2baJasper Van der Jeugt
tests/benchmarks/src/Data/Text/Benchmarks.hs+1−1
@@ -48,7 +48,7 @@ benchmarks = do
48 , Ordering.benchmark (tf "russian.txt")
48 , Ordering.benchmark (tf "russian.txt")
49 , Pure.benchmark (tf "japanese.txt")
49 , Pure.benchmark (tf "japanese.txt")
50 , ReadNumbers.benchmark (tf "numbers.txt")
50 , ReadNumbers.benchmark (tf "numbers.txt")
51− , Replace.benchmark (tf "russian.txt") sink "принимая" "своем"
51+ , Replace.benchmark (tf "russian.txt") "принимая" "своем"
52 , Search.benchmark (tf "russian.txt") "принимая"
52 , Search.benchmark (tf "russian.txt") "принимая"
53 , WordCount.benchmark (tf "russian.txt")
53 , WordCount.benchmark (tf "russian.txt")
54 ]
54 ]
tests/benchmarks/src/Data/Text/Benchmarks/Replace.hs+10−16
@@ -1,11 +1,10 @@
1−-- | Replace a string by another string in a file
1+-- | Replace a string by another string
2--
2--
3module Data.Text.Benchmarks.Replace
3module Data.Text.Benchmarks.Replace
4 ( benchmark
4 ( benchmark
5 ) where
5 ) where
6
6
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
15
14
16−benchmark :: FilePath -> Handle -> String -> String -> IO Benchmark
15+benchmark :: FilePath -> String -> String -> IO Benchmark
16+benchmark fp pat sub = do
17+ tl <- TL.readFile fp
18+ bl <- BL.readFile fp
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
24−
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
28− ]
22+ ]
29 where
23 where
30 tpat = TL.pack pat
24 tpat = TL.pack pat
31 tsub = TL.pack sub
25 tsub = TL.pack sub