-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ch28_SetBenchmark.hs
54 lines (41 loc) · 1.29 KB
/
Ch28_SetBenchmark.hs
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
module Main where
import Criterion.Main
import qualified Data.Map as M
import qualified Data.Set as S
bumpIt (i, v) = (i + 1, v + 1)
m :: M.Map Int Int
m = M.fromList $ take 10000 stream
where
stream = iterate bumpIt (0, 0)
s :: S.Set Int
s = S.fromList $ take 10000 stream
where
stream = iterate (+ 1) 0
membersMap :: Int -> Bool
membersMap i = M.member i m
membersSet :: Int -> Bool
membersSet i = S.member i s
insertMap :: Int -> M.Map Int Int
insertMap k = M.insert k 10001 m
insertSet :: Int -> S.Set Int
insertSet k = S.insert k s
partitionMap :: Int -> (M.Map Int Int, M.Map Int Int)
partitionMap i = M.partition (>i) m
partitionSet :: Int -> (S.Set Int, S.Set Int)
partitionSet i = S.partition (>i) s
mapMap :: Int -> M.Map Int Int
mapMap i = M.mapKeys (+i) m
mapSet :: Int -> S.Set Int
mapSet i = S.map (+i) s
main :: IO ()
main =
defaultMain
[ bench "map - member check" $ whnf membersMap 9999
, bench "set - member check" $ whnf membersSet 9999
, bench "map - insert check" $ whnf insertMap 10001
, bench "set - insert check" $ whnf insertSet 10001
, bench "map - partition check" $ whnf partitionMap 100
, bench "set - partition check" $ whnf partitionSet 100
, bench "map - map check" $ whnf mapMap 100
, bench "set - map check" $ whnf mapSet 100
]