File: Main.hs

package info (click to toggle)
haskell-text-builder 0.6.7.2-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 80 kB
  • sloc: haskell: 406; makefile: 5
file content (57 lines) | stat: -rw-r--r-- 1,422 bytes parent folder | download
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Main where

import Criterion.Main
import qualified Data.Text.Lazy as C
import qualified Data.Text.Lazy.Builder as B
import qualified Text.Builder as A
import Prelude

main :: IO ()
main =
  defaultMain
    $ [ subjectBenchmark "builderSubject" builderSubject,
        subjectBenchmark "lazyTextBuilderSubject" lazyTextBuilderSubject
      ]

subjectBenchmark :: String -> Subject -> Benchmark
subjectBenchmark title subject =
  bgroup title
    $ [ benchmark "Small input" smallSample subject,
        benchmark "Large input" largeSample subject
      ]

benchmark :: String -> Sample -> Subject -> Benchmark
benchmark title sample subject =
  bench title $ nf sample $ subject

data Subject
  = forall a. Subject (Text -> a) (a -> a -> a) a (a -> Text)

type Sample =
  Subject -> Text

builderSubject :: Subject
builderSubject =
  Subject A.text mappend mempty A.run

lazyTextBuilderSubject :: Subject
lazyTextBuilderSubject =
  Subject B.fromText mappend mempty (C.toStrict . B.toLazyText)

{-# NOINLINE smallSample #-}
smallSample :: Sample
smallSample (Subject text (<>) mempty run) =
  run
    $ text "abcd"
    <> (text "ABCD" <> text "Фываолдж")
    <> text "漢"

{-# NOINLINE largeSample #-}
largeSample :: Sample
largeSample (Subject text (<>) mempty run) =
  run
    $ foldl' (<>) mempty
    $ replicate 100000
    $ text "abcd"
    <> (text "ABCD" <> text "Фываолдж")
    <> text "漢"