{-# LANGUAGE BangPatterns #-}
-- | Replace a string by another string---- Tested in this benchmark:---- * Search and replace of a pattern in a text--
module Benchmarks.Replace
( benchmark
) where
import Criterion (Benchmark, bgroup, bench, nf)
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 :: FilePath -> String -> String -> IOBenchmarkbenchmarkfppatsub = do
tl <- TL.readFilefpbl <- BL.readFilefp
let !t = TL.toStricttl
!b = T.encodeUtf8treturn$bgroup"Replace" [
bench"Text"$nf (T.length.T.replacetpattsub) t
, bench"ByteString"$nf (BL.length.B.replacebpatbsub) b
, bench"LazyText"$nf (TL.length.TL.replacetlpattlsub) tl
, bench"LazyByteString"$nf (BL.length.BL.replaceblpatblsub) bl
]
where
tpat = T.packpattsub = T.packsubtlpat = TL.packpattlsub = TL.packsubbpat = T.encodeUtf8tpatbsub = T.encodeUtf8tsubblpat = B.concat$BL.toChunks$TL.encodeUtf8tlpatblsub = B.concat$BL.toChunks$TL.encodeUtf8tlsub