File: Equality.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 (38 lines) | stat: -rw-r--r-- 1,378 bytes parent folder | download | duplicates (5)
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
-- | Compare a string with a copy of itself that is identical except
-- for the last character.
--
-- Tested in this benchmark:
--
-- * Comparison of strings (Eq instance)
--
module Benchmarks.Equality
    ( benchmark
    ) where

import Criterion (Benchmark, bgroup, bench, whnf)
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Char8 as BL
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

benchmark :: FilePath -> IO Benchmark
benchmark fp = do
  b <- B.readFile fp
  bl1 <- BL.readFile fp
  -- A lazy bytestring is a list of chunks. When we do not explicitly create two
  -- different lazy bytestrings at a different address, the bytestring library
  -- will compare the chunk addresses instead of the chunk contents. This is why
  -- we read the lazy bytestring twice here.
  bl2 <- BL.readFile fp
  l <- readFile fp
  let t  = T.decodeUtf8 b
      tl = TL.decodeUtf8 bl1
  return $ bgroup "Equality"
    [ bench "Text" $ whnf (== T.init t `T.snoc` '\xfffd') t
    , bench "LazyText" $ whnf (== TL.init tl `TL.snoc` '\xfffd') tl
    , bench "ByteString" $ whnf (== B.init b `B.snoc` '\xfffd') b
    , bench "LazyByteString" $ whnf (== BL.init bl2 `BL.snoc` '\xfffd') bl1
    , bench "String" $ whnf (== init l ++ "\xfffd") l
    ]