-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.fs
More file actions
254 lines (189 loc) · 9.22 KB
/
Program.fs
File metadata and controls
254 lines (189 loc) · 9.22 KB
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
246
247
248
249
250
251
252
253
254
module ListBenchmarks
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
type MyRecord = { Id: int; Name: string }
module List =
let containsByExists x xs = List.exists ((=) x) xs
let inline containsByExistsInline x xs = List.exists ((=) x) xs
let inline containsMutable value (source: list<_>) =
let mutable xs = source
let mutable state = false
while (not state && not xs.IsEmpty) do
state <- value = xs.Head
xs <- xs.Tail
state
let inline containsTryFind value (source: list<_>) =
source |> List.tryFind (fun x -> value = x) |> Option.isSome
let inline outerContainsWhatIf value source =
let inline isMatch elem = elem = value
let rec innerContainsWhatIf e xs1 =
match xs1 with
| [] -> false
| h1 :: t1 -> isMatch h1 || innerContainsWhatIf e t1
innerContainsWhatIf value source
let inline test xs f =
for i in xs do
f i xs |> ignore
[<MemoryDiagnoser>]
type ListTests() =
let listOfInts = [ 1..1000 ]
let listOfStrings = listOfInts |> List.map (fun i -> i.ToString())
let listOfRecords =
listOfInts |> List.map (fun i -> { Id = i; Name = i.ToString() })
[<Benchmark(Description = "int - List.exists")>]
member _.IntListExists() =
test listOfInts (fun i -> List.exists (fun item -> item = i))
[<Benchmark(Description = "int - List.contains")>]
member _.IntListContains() =
test listOfInts List.contains
[<Benchmark(Description = "int - List.containsByExists")>]
member _.IntListContainsByExists() =
test listOfInts List.containsByExists
[<Benchmark(Description = "int - List.containsByExistsInline")>]
member _.IntListContainsByExistsInline() =
test listOfInts List.containsByExistsInline
[<Benchmark(Description = "int - List.containsMutable")>]
member _.IntListcontainsMutable() =
test listOfInts List.containsMutable
[<Benchmark(Description = "int - List.containsTryFind")>]
member _.IntListContainsTryFind() =
test listOfInts List.containsTryFind
[<Benchmark(Description = "int - List.outerContainsWhatIf")>]
member _.IntListOuterContainsWhatIf() =
test listOfInts List.outerContainsWhatIf
[<Benchmark(Description = "string - List.exists")>]
member _.StringListExists() =
test listOfStrings (fun i -> List.exists (fun item -> item = i))
[<Benchmark(Description = "string - List.contains")>]
member _.StringListContains() =
test listOfStrings List.contains
[<Benchmark(Description = "string - List.containsByExists")>]
member _.StringListContainsByExists() =
test listOfStrings List.containsByExists
[<Benchmark(Description = "string - List.containsByExistsInline")>]
member _.StringListContainsByExistsInline() =
test listOfStrings List.containsByExistsInline
[<Benchmark(Description = "string - List.containsMutable")>]
member _.StringListcontainsMutable() =
test listOfStrings List.containsMutable
[<Benchmark(Description = "string - List.containsTryFind")>]
member _.StringListContainsTryFind() =
test listOfStrings List.containsTryFind
[<Benchmark(Description = "string - List.outerContainsWhatIf")>]
member _.StringListOuterContainsWhatIf() =
test listOfStrings List.outerContainsWhatIf
[<Benchmark(Description = "record - List.exists")>]
member _.RecordListExists() =
test listOfRecords (fun i -> List.exists (fun item -> item.Id = i.Id))
[<Benchmark(Description = "record - List.contains")>]
member _.RecordListContains() =
test listOfRecords (fun i -> List.contains i)
[<Benchmark(Description = "record - List.containsByExists")>]
member _.RecordListContainsByExists() =
test listOfRecords (fun i -> List.containsByExists i)
[<Benchmark(Description = "record - List.containsByExistsInline")>]
member _.RecordListContainsByExistsInline() =
test listOfRecords (fun i -> List.containsByExistsInline i)
[<Benchmark(Description = "record - List.containsMutable")>]
member _.RecordListcontainsMutable() =
test listOfRecords (fun i -> List.containsMutable i)
[<Benchmark(Description = "record - List.containsTryFind")>]
member _.RecordListContainsTryFind() =
test listOfRecords (fun i -> List.containsTryFind i)
[<Benchmark(Description = "record - List.outerContainsWhatIf")>]
member _.RecordListOuterContainsWhatIf() =
test listOfRecords (fun i -> List.outerContainsWhatIf i)
module Array =
let containsByExists x xs = Array.exists ((=) x) xs
let inline containsByExistsInline x xs = Array.exists ((=) x) xs
[<MemoryDiagnoser>]
type ArrayTests() =
let arrayOfInts = [| 1..1000 |]
let arrayOfStrings = arrayOfInts |> Array.map (fun i -> i.ToString())
let arrayOfRecords =
arrayOfInts |> Array.map (fun i -> { Id = i; Name = i.ToString() })
[<Benchmark(Description = "int - Array.exists")>]
member _.IntArrayExists() =
test arrayOfInts (fun i -> Array.exists (fun item -> item = i))
[<Benchmark(Description = "int - Array.contains")>]
member _.IntArrayContains() =
test arrayOfInts Array.contains
[<Benchmark(Description = "int - Array.containsByExists")>]
member _.IntArrayContainsByExists() =
test arrayOfInts Array.containsByExists
[<Benchmark(Description = "int - Array.containsByExistsInline")>]
member _.IntArrayContainsByExistsInline() =
test arrayOfInts Array.containsByExistsInline
[<Benchmark(Description = "string - Array.exists")>]
member _.StringArrayExists() =
test arrayOfStrings (fun i -> Array.exists (fun item -> item = i))
[<Benchmark(Description = "string - Array.contains")>]
member _.StringArrayContains() =
test arrayOfStrings Array.contains
[<Benchmark(Description = "string - Array.containsByExists")>]
member _.StringArrayContainsByExists() =
test arrayOfStrings Array.containsByExists
[<Benchmark(Description = "string - Array.containsByExistsInline")>]
member _.StringArrayContainsByExistsInline() =
test arrayOfStrings Array.containsByExistsInline
[<Benchmark(Description = "record - Array.exists")>]
member _.RecordArrayExists() =
test arrayOfRecords (fun i -> Array.exists (fun item -> item.Id = i.Id))
[<Benchmark(Description = "record - Array.contains")>]
member _.RecordArrayContains() =
test arrayOfRecords (fun i -> Array.contains i)
[<Benchmark(Description = "record - Array.containsByExists")>]
member _.RecordArrayContainsByExists() =
test arrayOfRecords (fun i -> Array.containsByExists i)
[<Benchmark(Description = "record - Array.containsByExistsInline")>]
member _.RecordArrayContainsByExistsInline() =
test arrayOfRecords (fun i -> Array.containsByExistsInline i)
module Seq =
let containsByExists x xs = Seq.exists ((=) x) xs
let inline containsByExistsInline x xs = Seq.exists ((=) x) xs
[<MemoryDiagnoser>]
type SeqTests() =
let seqOfInts = seq { 1..1000 }
let seqOfStrings = seqOfInts |> Seq.map (fun i -> i.ToString())
let seqOfRecords =
seqOfInts |> Seq.map (fun i -> { Id = i; Name = i.ToString() })
[<Benchmark(Description = "int - Seq.exists")>]
member _.IntSeqExists() =
test seqOfInts (fun i -> Seq.exists (fun item -> item = i))
[<Benchmark(Description = "int - Seq.contains")>]
member _.IntSeqContains() =
test seqOfInts Seq.contains
[<Benchmark(Description = "int - Seq.containsByExists")>]
member _.IntSeqContainsByExists() =
test seqOfInts Seq.containsByExists
[<Benchmark(Description = "int - Seq.containsByExistsInline")>]
member _.IntSeqContainsByExistsInline() =
test seqOfInts Seq.containsByExistsInline
[<Benchmark(Description = "string - Seq.exists")>]
member _.StringSeqExists() =
test seqOfStrings (fun i -> Seq.exists (fun item -> item = i))
[<Benchmark(Description = "string - Seq.contains")>]
member _.StringSeqContains() =
test seqOfStrings Seq.contains
[<Benchmark(Description = "string - Seq.containsByExists")>]
member _.StringSeqContainsByExists() =
test seqOfStrings Seq.containsByExists
[<Benchmark(Description = "string - Seq.containsByExistsInline")>]
member _.StringSeqContainsByExistsInline() =
test seqOfStrings Seq.containsByExistsInline
[<Benchmark(Description = "record - Seq.exists")>]
member _.RecordSeqExists() =
test seqOfRecords (fun i -> Seq.exists (fun item -> item.Id = i.Id))
[<Benchmark(Description = "record - Seq.contains")>]
member _.RecordSeqContains() =
test seqOfRecords (fun i -> Seq.contains i)
[<Benchmark(Description = "record - Seq.containsByExists")>]
member _.RecordSeqContainsByExists() =
test seqOfRecords (fun i -> Seq.containsByExists i)
[<Benchmark(Description = "record - Seq.containsByExistsInline")>]
member _.RecordSeqContainsByExistsInline() =
test seqOfRecords (fun i -> Seq.containsByExistsInline i)
BenchmarkRunner.Run<ListTests>()
BenchmarkRunner.Run<ArrayTests>()
BenchmarkRunner.Run<SeqTests>()
//BenchmarkRunner.Run([| typeof<ListTests>; typeof<ArrayTests>; typeof<SeqTests> |])