File: Bench.hs

package info (click to toggle)
haskell-pretty-simple 4.1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 204 kB
  • sloc: haskell: 1,075; makefile: 4
file content (41 lines) | stat: -rw-r--r-- 1,093 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
39
40
41

module Main where

import Data.Monoid ((<>))
import Data.Text.Lazy (Text)
import Criterion.Main (Benchmark, bench, bgroup, defaultMain, nf)
import Text.Pretty.Simple (pShow)

import Example.Data (foo, bar, baz)

main :: IO ()
main =
  defaultMain
    [ bgroup
        "pShow"
        [ bench "Foo" $ nf pShow foo
        , bench "Bar" $ nf pShow bar
        , bench "Baz" $ nf pShow baz
        ]
    , bgroup "recursive deeply-nested data structure" (fmap nestTest [22..25])
    ]

data ExampleExpr
  = A
  | B ExampleExpr
  | C [ExampleExpr]
  deriving (Show)

nest :: ExampleExpr -> Int -> ExampleExpr
nest expr 0 = expr
nest expr n = nest (B expr) (n - 1)

-- | There was a bug in the pretty-simple code that caused deeply nested data
-- structures to have an exponential runtime.  Effectively, the runtime doubled
-- at level.  The following benchmark is to make sure that we don't
-- accidentally introduce this exponential runtime again.
nestTest :: Int -> Benchmark
nestTest n = bench ("level " <> show n) $ nf test n
  where
    test :: Int -> Text
    test = pShow . nest (C [A,A])