summaryrefslogtreecommitdiff
path: root/Haskell-book/28/Bench/app/Main.hs
blob: c3c602f183c73721a36500201c879fe6a4540dfa (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 qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as U

genList :: Int -> [(String, Int)]
genList n = go n []
    where go 0 xs = ("0", 0) : xs
          go n' xs = go (n' - 1) ((show n', n') : xs)

pairList :: [(String, Int)]
pairList = genList 9001

testMap :: M.Map String Int
testMap = M.fromList pairList

testSet :: S.Set String
testSet = S.fromList $ fmap fst pairList

slice :: Int -> Int -> [a] -> [a]
slice from len xs = take len (drop from xs)

boxed :: V.Vector Int
boxed = V.fromList [1..1000]

unboxed :: U.Vector Int
unboxed = U.fromList [1..1000]

main :: IO ()
main = defaultMain
    [ bench "slicing unboxed vector" $
        whnf (U.head . U.slice 100 900) unboxed
    , bench "slicing boxed vector" $
        whnf (V.head . V.slice 100 900) boxed
    , bench "lookup one thing, set" $
        whnf (S.member "doesntExist") testSet
    , bench "insert one thing, set" $
        whnf (S.insert "doesntExist" ) S.empty
    , bench "lookup one thing, map" $
        whnf (M.lookup "doesntExist") testMap
    , bench "insert one thing, map" $
        whnf (M.insert ("doesntExist", 0)) M.empty
    ]