File: Replace.hs

package info (click to toggle)
haskell-text 1.2.0.6-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 900 kB
  • sloc: haskell: 9,299; ansic: 238; python: 87; ruby: 84; sh: 49; makefile: 29
file content (43 lines) | stat: -rw-r--r-- 1,451 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{-# 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 -> IO Benchmark
benchmark fp pat sub = do
    tl <- TL.readFile fp
    bl <- BL.readFile fp
    let !t = TL.toStrict tl
        !b = T.encodeUtf8 t
    return $ bgroup "Replace" [
          bench "Text"           $ nf (T.length . T.replace tpat tsub) t
        , bench "ByteString"     $ nf (BL.length . B.replace bpat bsub) b
        , bench "LazyText"       $ nf (TL.length . TL.replace tlpat tlsub) tl
        , bench "LazyByteString" $ nf (BL.length . BL.replace blpat blsub) bl
        ]
  where
    tpat  = T.pack pat
    tsub  = T.pack sub
    tlpat = TL.pack pat
    tlsub = TL.pack sub
    bpat = T.encodeUtf8 tpat
    bsub = T.encodeUtf8 tsub
    blpat = B.concat $ BL.toChunks $ TL.encodeUtf8 tlpat
    blsub = B.concat $ BL.toChunks $ TL.encodeUtf8 tlsub