-
Notifications
You must be signed in to change notification settings - Fork 4
/
Time.hs
245 lines (233 loc) · 7.2 KB
/
Time.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ExistentialQuantification #-}
module Main (main) where
import Control.DeepSeq
import Control.Monad
import Criterion.Main
import Criterion.Types
import qualified Data.BloomFilter.Easy
import qualified Data.DAWG.Packed
import qualified Data.HashSet
import qualified Data.IntSet
import qualified Data.Set
import System.Directory
import System.Random
data InsertInt = forall f. NFData f => InsertInt String (Int -> f)
data FromListS =
forall f. NFData f =>
FromListS String
([String] -> f)
data Intersection = forall f. NFData f =>
Intersection String ([Int] -> f) (f -> f -> f)
data Member =
forall f. (NFData f) =>
Member String
([Int] -> f)
(Int -> f -> Bool)
data MemberS =
forall f. (NFData f) =>
MemberS String
([String] -> f)
(String -> f -> Bool)
main :: IO ()
main = do
let fp = "out.csv"
exists <- doesFileExist fp
when exists (removeFile fp)
defaultMainWith
defaultConfig {csvFile = Just fp}
[ bgroup
"Insert Int (Randomized)"
(insertInts
[ InsertInt "Data.Set" insertSet
, InsertInt "Data.HashSet" insertHashSet
, InsertInt "Data.IntSet" insertIntSet
])
, bgroup
"Intersection (Randomized)"
(intersection
[ Intersection
"Data.Set"
Data.Set.fromList
Data.Set.intersection
, Intersection
"Data.HashSet"
Data.HashSet.fromList
Data.HashSet.intersection
, Intersection
"Data.IntSet"
Data.IntSet.fromList
Data.IntSet.intersection
])
, bgroup
"Member Int (Randomized)"
(memberRandomized
[ Member
"Data.Set"
Data.Set.fromList
Data.Set.member
, Member
"Data.HashSet"
Data.HashSet.fromList
Data.HashSet.member
, Member
"Data.IntSet"
Data.IntSet.fromList
Data.IntSet.member
])
, bgroup
"Member Int (Randomized, false positive rate 0.1)"
(memberRandomized
[ Member
"Data.Set"
Data.Set.fromList
Data.Set.member
, Member
"Data.HashSet"
Data.HashSet.fromList
Data.HashSet.member
, Member
"Data.IntSet"
Data.IntSet.fromList
Data.IntSet.member
, Member
"Data.BloomFilter"
(Data.BloomFilter.Easy.easyList 0.1)
Data.BloomFilter.Easy.elem
])
, bgroup
"Member String (Randomized)"
(memberRandomizedS
[ MemberS
"Data.Set"
Data.Set.fromList
Data.Set.member
, MemberS
"Data.HashSet"
Data.HashSet.fromList
Data.HashSet.member
, MemberS
"Data.DAWG.Packed"
Data.DAWG.Packed.fromList
Data.DAWG.Packed.member
])
, bgroup
"Member String (Randomized, false positive rate 0.1)"
(memberRandomizedS
[ MemberS
"Data.Set"
Data.Set.fromList
Data.Set.member
, MemberS
"Data.HashSet"
Data.HashSet.fromList
Data.HashSet.member
, MemberS
"Data.DAWG.Packed"
Data.DAWG.Packed.fromList
Data.DAWG.Packed.member
, MemberS
"Data.BloomFilter"
(Data.BloomFilter.Easy.easyList 0.1)
Data.BloomFilter.Easy.elem
])
, bgroup
"FromList String (Monotonic)"
(insertSMonotonic
[ FromListS "Data.Set" Data.Set.fromList
, FromListS "Data.HashSet" Data.HashSet.fromList
, FromListS "Data.DAWG.Packed" Data.DAWG.Packed.fromList
])
, bgroup
"FromList String (Randomized)"
(insertSRandomized
[ FromListS "Data.Set" Data.Set.fromList
, FromListS "Data.HashSet" Data.HashSet.fromList
, FromListS "Data.DAWG.Packed" Data.DAWG.Packed.fromList
])
]
where
insertInts funcs =
[ env
(let !elems =
force (take i (randoms (mkStdGen 0) :: [Int]))
in pure elems)
(\_ -> bench (title ++ ":" ++ show i) $ nf func i)
| i <- [10, 100, 1000, 10000]
, InsertInt title func <- funcs
]
intersection funcs =
[ env
(let !args =
force
( build (take i (randoms (mkStdGen 0) :: [Int]))
, build (take i (randoms (mkStdGen 1) :: [Int])))
in pure args)
(bench (title ++ ":" ++ show i) . nf (uncurry intersect))
| i <- [10, 100, 1000, 10000, 100000, 1000000]
, Intersection title build intersect <- funcs
]
insertSRandomized funcs =
[ env
(let !elems =
force
(map
show
(take i (randoms (mkStdGen 0) :: [Int])))
in pure elems)
(bench (title ++ ":" ++ show i) . nf func)
| i <- [10, 100, 1000, 10000]
, FromListS title func <- funcs
]
insertSMonotonic funcs =
[ env
(let !elems =
force
(map
show
[1 :: Int .. i])
in pure elems)
(bench (title ++ ":" ++ show i) . nf func)
| i <- [10000]
, FromListS title func <- funcs
]
memberRandomized funcs =
[ env
(let list = take i (randoms (mkStdGen 0) :: [Int])
!key = list !! div i 2
!elems = force (fromList list)
in pure (elems, key))
(\(~(elems, key)) ->
bench (title ++ ":" ++ show i) $ nf (`func` elems) key)
| i <- [10, 100, 1000, 10000, 100000, 1000000]
, Member title fromList func <- funcs
]
memberRandomizedS funcs =
[ env
(let list = take i (map show (randoms (mkStdGen 0) :: [Int]))
!key = list !! div i 2
!elems = force (fromList list)
in pure (elems, key))
(\(~(elems, key)) ->
bench (title ++ ":" ++ show i) $ nf (`func` elems) key)
| i <- [10, 100, 1000, 10000, 100000, 1000000]
, MemberS title fromList func <- funcs
]
--------------------------------------------------------------------------------
-- Insert Int
insertSet :: Int -> Data.Set.Set Int
insertSet n0 = go n0 mempty
where
go 0 acc = acc
go n !acc = go (n - 1) (Data.Set.insert n acc)
insertHashSet :: Int -> Data.HashSet.HashSet Int
insertHashSet n0 = go n0 mempty
where
go 0 acc = acc
go n !acc = go (n - 1) (Data.HashSet.insert n acc)
insertIntSet :: Int -> Data.IntSet.IntSet
insertIntSet n0 = go n0 mempty
where
go 0 acc = acc
go n !acc = go (n - 1) (Data.IntSet.insert n acc)