File: BenchHexadecimal.hs

package info (click to toggle)
haskell-text-builder-linear 0.1.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 236 kB
  • sloc: haskell: 2,021; makefile: 3
file content (70 lines) | stat: -rw-r--r-- 2,308 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
58
59
60
61
62
63
64
65
66
67
68
69
70
-- |
-- Copyright:   (c) 2022 Andrew Lelechenko
-- Licence:     BSD3
-- Maintainer:  Andrew Lelechenko <andrew.lelechenko@gmail.com>

module BenchHexadecimal (benchHexadecimal) where

import qualified Data.ByteString as B
import qualified Data.ByteString.Builder as B
import qualified Data.Text as T
import Data.Text.Builder.Linear.Buffer
import Data.Text.Lazy (toStrict)
import Data.Text.Lazy.Builder (toLazyText)
import Data.Text.Lazy.Builder.Int (hexadecimal)
import Test.Tasty.Bench

#ifdef MIN_VERSION_text_builder
import qualified Text.Builder
#endif

word :: Word
word = 123456789123456789

benchLazyBuilder ∷ Word → T.Text
benchLazyBuilder = toStrict . toLazyText . go mempty
  where
    go !acc 0 = acc
    go !acc n = let i = n * word in go (hexadecimal i <> (acc <> hexadecimal i)) (n - 1)

benchLazyBuilderBS ∷ Word → B.ByteString
benchLazyBuilderBS = B.toStrict . B.toLazyByteString . go mempty
  where
    go !acc 0 = acc
    go !acc n = go (B.wordHex n <> (acc <> B.wordHex n)) (n - 1)

#ifdef MIN_VERSION_text_builder
benchStrictBuilder ∷ Word → T.Text
benchStrictBuilder = Text.Builder.run . go mempty
  where
    go !acc 0 = acc
    go !acc n = let i = n * word in go (Text.Builder.hexadecimal i <> (acc <> Text.Builder.hexadecimal i)) (n - 1)
#endif

benchLinearBuilderWord ∷ Word → T.Text
benchLinearBuilderWord m = runBuffer (\b → go b m)
  where
    go ∷ Buffer ⊸ Word → Buffer
    go !acc 0 = acc
    go !acc n = let i = n * word in go (i &<| (acc |>& i)) (n - 1)

benchLinearBuilderInt ∷ Word → T.Text
benchLinearBuilderInt m = runBuffer (\b → go b (fromIntegral m))
  where
    go ∷ Buffer ⊸ Int → Buffer
    go !acc 0 = acc
    go !acc n = let i = n * fromIntegral word in go (i &<| (acc |>& i)) (n - 1)

benchHexadecimal ∷ Benchmark
benchHexadecimal = bgroup "Hexadecimal" $ map mkGroup [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6]

mkGroup :: Word → Benchmark
mkGroup n = bgroup (show n)
  [ bench "Data.Text.Lazy.Builder" $ nf benchLazyBuilder n
  , bench "Data.ByteString.Builder" $ nf benchLazyBuilderBS n
#ifdef MIN_VERSION_text_builder
  , bench "Text.Builder" $ nf benchStrictBuilder n
#endif
  , bench "Data.Text.Builder.Linear (Word)" $ nf benchLinearBuilderWord n
  , bench "Data.Text.Builder.Linear (Int)" $ nf benchLinearBuilderInt n
  ]