File: FasterUnsafeText.hs

package info (click to toggle)
haskell-prettyprinter 1.7.1-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 372 kB
  • sloc: haskell: 2,453; ansic: 16; makefile: 6
file content (35 lines) | stat: -rw-r--r-- 947 bytes parent folder | download | duplicates (2)
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
{-# LANGUAGE OverloadedStrings #-}

module Main (main) where



import           Gauge.Main
import           Data.Char
import           Data.Text              (Text)
import qualified Data.Text              as T
import           Prettyprinter.Internal



-- The old implementation. Performance isn’t much worse to be honest, mostly
-- well within a σ.
alternative :: Text -> Doc ann
alternative t = case T.length t of
    0 -> Empty
    1 -> Char (T.head t)
    n -> Text n t

current :: Text -> Doc ann
current = unsafeTextWithoutNewlines

main :: IO ()
main = defaultMain [ benchText (letters n) | n <- [0,1,2,3,5,10,50,100] ]

letters :: Int -> Text
letters n = T.pack (take n (filter isAlpha [minBound ..]))

benchText :: Text -> Benchmark
benchText input = bgroup (show (pretty (T.length input) <+> plural "letter" "letters" (T.length input)))
    [ bench "alternative" (whnf alternative input)
    , bench "current" (whnf current input) ]