summaryrefslogtreecommitdiff
path: root/Haskell-book/28/DifferenceList/app/Main.hs
blob: 8ece171c79f4a1b81c2f1bef9499bb105efdc3ad (plain)
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
module Main where

import Criterion.Main
import Data.DList
import qualified Data.Queue as Q
import qualified Data.Sequence as S

schlemiel :: Int -> [Int]
schlemiel i = go i []
    where go 0 xs = xs
          go n xs = go (n - 1) ([n] ++ xs)

constructDlist :: Int -> [Int]
constructDlist i = toList $ go i empty
    where go 0 xs = xs
          go n xs = go (n - 1) (singleton n `append` xs)

processQueue :: Int -> Q.Queue Int
processQueue i = clear $ Q.pop $ fill i Q.empty
    where fill 0 xs = xs
          fill n xs = fill (n - 1) (Q.push n xs)
          clear Nothing = Q.empty
          clear (Just xs) = clear $ Q.pop $ snd xs

processList :: Int -> [Int]
processList i = go (schlemiel i)
    where go [] = []
          go (x:xs) = xs

processSeq :: Int -> S.Seq Int
processSeq i = go $ S.fromList $ schlemiel i
    where go xs = if S.null xs then xs else go (S.deleteAt 0 xs)

main :: IO ()
main = defaultMain
    [ bench "concat list" $
      whnf schlemiel 123456
    , bench "concat dlist" $
      whnf constructDlist 123456
    , bench "process queue" $
      whnf processQueue 12345
    , bench "process list" $
      whnf processList 12345
    , bench "process sequence" $
      whnf processSeq 12345
    ]