on master help sign in
Purify Replace benchmark
commit60ab2bafd5ec3402f8a88734da2bbbd41aa7e2c3 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateFri 10 Jun 2011, 17:29 (15 years ago) parentcc24e80
2 files changed+11−17
tests/benchmarks/src/Data/Text/Benchmarks.hs+1−1 tests/benchmarks/src/Data/Text/Benchmarks/Replace.hs+10−16
tests/benchmarks/src/Data/Text/Benchmarks.hs+1−1
@@ -48,7 +48,7 @@ benchmarks = do
4848 , Ordering.benchmark (tf "russian.txt")
4949 , Pure.benchmark (tf "japanese.txt")
5050 , ReadNumbers.benchmark (tf "numbers.txt")
51− , Replace.benchmark (tf "russian.txt") sink "принимая" "своем"
51+ , Replace.benchmark (tf "russian.txt") "принимая" "своем"
5252 , Search.benchmark (tf "russian.txt") "принимая"
5353 , WordCount.benchmark (tf "russian.txt")
5454 ]
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
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