forked from matthewcrews/FSharpPerformance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.fs
More file actions
554 lines (431 loc) · 18.2 KB
/
Program.fs
File metadata and controls
554 lines (431 loc) · 18.2 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
open System
open Argu
open BenchmarkDotNet.Diagnosers
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
open TopologicalSort
module Data =
let nodeCount = 20
let graphCount = 1_000
let rngSeed = 123
let randomEdgeCount =
[|
1
1
1
1
1
1
2
2
2
3
|]
module Version1 =
open TopologicalSort.Version1
// Create a new random number generator with the same seed
let rng = Random rngSeed
// Create the list of Nodes that we will use
let nodes =
[for i in 0 .. nodeCount - 1 ->
{ Name = $"Node{i}" }]
// Generate the random Graphs we will solve
let graphs =
[for _ in 1 .. graphCount ->
[for sourceIdx in 0 .. nodeCount - 2 do
// We use a weighted distribution for the number of edges
for _ in 1 .. randomEdgeCount[(rng.Next randomEdgeCount.Length)] do
let targetIdx = rng.Next (sourceIdx + 1, nodeCount - 1)
{ Source = nodes[sourceIdx]; Target = nodes[targetIdx] }]
|> List.distinct
|> Graph
]
module Version2 =
open TopologicalSort.Version2
// Create a new random number generator with the same seed
let rng = Random rngSeed
// Create the list of Nodes that we will use
let nodes =
[for i in 0 .. nodeCount - 1 ->
{ Name = $"Node{i}" }]
// Generate the random Graphs we will solve
let graphs =
[for _ in 1 .. graphCount ->
[for sourceIdx in 0 .. nodeCount - 2 do
// We use a weighted distribution for the number of edges
for _ in 1 .. randomEdgeCount[(rng.Next randomEdgeCount.Length)] do
let targetIdx = rng.Next (sourceIdx + 1, nodeCount - 1)
{ Source = nodes[sourceIdx]; Target = nodes[targetIdx] }]
|> List.distinct
|> Graph.create
]
module Version3 =
open TopologicalSort.Version3
// Create a new random number generator with the same seed
let rng = Random rngSeed
// Create the list of Nodes that we will use
let nodes =
[for i in 0 .. nodeCount - 1 ->
{ Name = $"Node{i}" }]
// Generate the random Graphs we will solve
let graphs =
[for _ in 1 .. graphCount ->
[for sourceIdx in 0 .. nodeCount - 2 do
// We use a weighted distribution for the number of edges
for _ in 1 .. randomEdgeCount[(rng.Next randomEdgeCount.Length)] do
let targetIdx = rng.Next (sourceIdx + 1, nodeCount - 1)
{ Source = nodes[sourceIdx]; Target = nodes[targetIdx] }]
|> List.distinct
|> Graph.create
]
module Version4 =
open TopologicalSort.Version4
// Create a new random number generator with the same seed
let rng = Random rngSeed
// Create the list of Nodes that we will use
let nodes =
[for i in 0 .. nodeCount - 1 ->
Node.create i]
// Generate the random Graphs we will solve
let graphs =
[for _ in 1 .. graphCount ->
[for sourceIdx in 0 .. nodeCount - 2 do
// We use a weighted distribution for the number of edges
for _ in 1 .. randomEdgeCount[(rng.Next randomEdgeCount.Length)] do
let targetIdx = rng.Next (sourceIdx + 1, nodeCount - 1)
let source = nodes[sourceIdx]
let target = nodes[targetIdx]
Edge.create source target ]
|> List.distinct
|> Graph.create
]
module Version5 =
open TopologicalSort.Version5
// Create a new random number generator with the same seed
let rng = Random rngSeed
// Create the list of Nodes that we will use
let nodes =
[for i in 0 .. nodeCount - 1 ->
Node.create i]
// Generate the random Graphs we will solve
let graphs =
[for _ in 1 .. graphCount ->
[|for sourceIdx in 0 .. nodeCount - 2 do
// We use a weighted distribution for the number of edges
for _ in 1 .. randomEdgeCount[(rng.Next randomEdgeCount.Length)] do
let targetIdx = rng.Next (sourceIdx + 1, nodeCount - 1)
let source = nodes[sourceIdx]
let target = nodes[targetIdx]
Edge.create source target |]
|> Array.distinct
|> Graph.create
]
module Version6 =
open TopologicalSort.Version6
// Create a new random number generator with the same seed
let rng = Random rngSeed
// Create the list of Nodes that we will use
let nodes =
[for i in 0 .. nodeCount - 1 ->
Node.create i]
// Generate the random Graphs we will solve
let graphs =
[for _ in 1 .. graphCount ->
[|for sourceIdx in 0 .. nodeCount - 2 do
// We use a weighted distribution for the number of edges
for _ in 1 .. randomEdgeCount[(rng.Next randomEdgeCount.Length)] do
let targetIdx = rng.Next (sourceIdx + 1, nodeCount - 1)
let source = nodes[sourceIdx]
let target = nodes[targetIdx]
Edge.create source target |]
|> Array.distinct
|> Graph.create
]
module Version7 =
open TopologicalSort.Version7
// Create a new random number generator with the same seed
let rng = Random rngSeed
// Create the list of Nodes that we will use
let nodes =
[for i in 0 .. nodeCount - 1 ->
Node.create i]
// Generate the random Graphs we will solve
let graphs =
[for _ in 1 .. graphCount ->
[|for sourceIdx in 0 .. nodeCount - 2 do
// We use a weighted distribution for the number of edges
for _ in 1 .. randomEdgeCount[(rng.Next randomEdgeCount.Length)] do
let targetIdx = rng.Next (sourceIdx + 1, nodeCount - 1)
let source = nodes[sourceIdx]
let target = nodes[targetIdx]
Edge.create source target |]
|> Array.distinct
|> Graph.create
]
module Version8 =
open TopologicalSort.Version8
// Create a new random number generator with the same seed
let rng = Random rngSeed
// Create the list of Nodes that we will use
let nodes =
[for i in 0 .. nodeCount - 1 ->
Node.create i]
// Generate the random Graphs we will solve
let graphs =
[for _ in 1 .. graphCount ->
[|for sourceIdx in 0 .. nodeCount - 2 do
// We use a weighted distribution for the number of edges
for _ in 1 .. randomEdgeCount[(rng.Next randomEdgeCount.Length)] do
let targetIdx = rng.Next (sourceIdx + 1, nodeCount - 1)
let source = nodes[sourceIdx]
let target = nodes[targetIdx]
Edge.create source target |]
|> Array.distinct
|> Graph.create
]
module Version9 =
open TopologicalSort.Version9
// Create a new random number generator with the same seed
let rng = Random rngSeed
// Create the list of Nodes that we will use
let nodes =
[for i in 0 .. nodeCount - 1 ->
Node.create i]
// Generate the random Graphs we will solve
let graphs =
[for _ in 1 .. graphCount ->
[|for sourceIdx in 0 .. nodeCount - 2 do
// We use a weighted distribution for the number of edges
for _ in 1 .. randomEdgeCount[(rng.Next randomEdgeCount.Length)] do
let targetIdx = rng.Next (sourceIdx + 1, nodeCount - 1)
let source = nodes[sourceIdx]
let target = nodes[targetIdx]
Edge.create source target |]
|> Array.distinct
|> Graph.create
]
module Version10 =
open TopologicalSort.Version10
// Create a new random number generator with the same seed
let rng = Random rngSeed
// Create the list of Nodes that we will use
let nodes =
[for i in 0 .. nodeCount - 1 ->
Node.create i]
// Generate the random Graphs we will solve
let graphs =
[for _ in 1 .. graphCount ->
[|for sourceIdx in 0 .. nodeCount - 2 do
// We use a weighted distribution for the number of edges
for _ in 1 .. randomEdgeCount[(rng.Next randomEdgeCount.Length)] do
let targetIdx = rng.Next (sourceIdx + 1, nodeCount - 1)
let source = nodes[sourceIdx]
let target = nodes[targetIdx]
Edge.create source target |]
|> Array.distinct
|> Graph.create
]
module Version11 =
open TopologicalSort.Version11
// Create a new random number generator with the same seed
let rng = Random rngSeed
// Create the list of Nodes that we will use
let nodes =
[for i in 0 .. nodeCount - 1 ->
Node.create i]
// Generate the random Graphs we will solve
let graphs =
[for _ in 1 .. graphCount ->
[|for sourceIdx in 0 .. nodeCount - 2 do
// We use a weighted distribution for the number of edges
for _ in 1 .. randomEdgeCount[(rng.Next randomEdgeCount.Length)] do
let targetIdx = rng.Next (sourceIdx + 1, nodeCount - 1)
let source = nodes[sourceIdx]
let target = nodes[targetIdx]
Edge.create source target |]
|> Array.distinct
|> Graph.create
]
[<MemoryDiagnoser>]
[<HardwareCounters(
HardwareCounter.BranchMispredictions,
HardwareCounter.BranchInstructions,
HardwareCounter.CacheMisses)>]
// [<DisassemblyDiagnoser>]
type Benchmarks () =
[<Benchmark>]
member _.V01 () =
let mutable result = None
for graph in Data.Version1.graphs do
// I separate the assignment so I can set a breakpoint in debugging
let sortedOrder = Version1.sort graph
result <- sortedOrder
result
[<Benchmark>]
member _.V02 () =
let mutable result = None
for graph in Data.Version2.graphs do
// I separate the assignment so I can set a breakpoint in debugging
let sortedOrder = Version2.sort graph
result <- sortedOrder
result
[<Benchmark>]
member _.V03 () =
let mutable result = None
for graph in Data.Version3.graphs do
// I separate the assignment so I can set a breakpoint in debugging
let sortedOrder = Version3.sort graph
result <- sortedOrder
result
// [<Benchmark>]
member _.V04 () =
let mutable result = None
for graph in Data.Version4.graphs do
// I separate the assignment so I can set a breakpoint in debugging
let sortedOrder = Version4.sort graph
result <- sortedOrder
result
// [<Benchmark>]
member _.V05 () =
let mutable result = None
for graph in Data.Version5.graphs do
// I separate the assignment so I can set a breakpoint in debugging
let sortedOrder = Version5.sort graph
result <- sortedOrder
result
// [<Benchmark>]
member _.V06 () =
let mutable result = None
for graph in Data.Version6.graphs do
// I separate the assignment so I can set a breakpoint in debugging
let sortedOrder = Version6.sort graph
result <- sortedOrder
result
// [<Benchmark>]
member _.V07 () =
let mutable result = None
for graph in Data.Version7.graphs do
// I separate the assignment so I can set a breakpoint in debugging
let sortedOrder = Version7.sort graph
result <- sortedOrder
result
// [<Benchmark>]
member _.V08 () =
let mutable result = None
for graph in Data.Version8.graphs do
// I separate the assignment so I can set a breakpoint in debugging
let sortedOrder = Version8.sort graph
result <- sortedOrder
result
// [<Benchmark>]
member _.V09 () =
let mutable result = None
for graph in Data.Version9.graphs do
// I separate the assignment so I can set a breakpoint in debugging
let sortedOrder = Version9.sort graph
result <- sortedOrder
result
// [<Benchmark>]
member _.V10 () =
let mutable result = None
for graph in Data.Version10.graphs do
// I separate the assignment so I can set a breakpoint in debugging
let sortedOrder = Version10.sort graph
result <- sortedOrder
result
// [<Benchmark>]
member _.V11 () =
let mutable result = ValueNone
for graph in Data.Version11.graphs do
// I separate the assignment so I can set a breakpoint in debugging
let sortedOrder = Version11.Graph.GraphType.Sort &graph
result <- sortedOrder
result
let profile (version: string) loopCount =
let b = Benchmarks ()
let mutable result = 0
match version.ToLower() with
| "v01" ->
for i in 1 .. loopCount do
match b.V01 () with
| Some order -> result <- result + 1
| None -> result <- result - 1
| "v02" ->
for i in 1 .. loopCount do
match b.V02 () with
| Some order -> result <- result + 1
| None -> result <- result - 1
| "v03" ->
for i in 1 .. loopCount do
match b.V03 () with
| Some order -> result <- result + 1
| None -> result <- result - 1
| "v04" ->
for i in 1 .. loopCount do
match b.V04 () with
| Some order -> result <- result + 1
| None -> result <- result - 1
| "v05" ->
for i in 1 .. loopCount do
match b.V05 () with
| Some order -> result <- result + 1
| None -> result <- result - 1
| "v06" ->
for i in 1 .. loopCount do
match b.V06 () with
| Some order -> result <- result + 1
| None -> result <- result - 1
| "v07" ->
for i in 1 .. loopCount do
match b.V07 () with
| Some order -> result <- result + 1
| None -> result <- result - 1
| "v08" ->
for i in 1 .. loopCount do
match b.V08 () with
| Some order -> result <- result + 1
| None -> result <- result - 1
| "v09" ->
for i in 1 .. loopCount do
match b.V09 () with
| Some order -> result <- result + 1
| None -> result <- result - 1
| "v10" ->
for i in 1 .. loopCount do
match b.V10 () with
| Some order -> result <- result + 1
| None -> result <- result - 1
| "v11" ->
for i in 1 .. loopCount do
match b.V11 () with
| ValueSome order -> result <- result + 1
| ValueNone -> result <- result - 1
| unknownVersion -> failwith $"Unknown version: {unknownVersion}"
result
[<RequireQualifiedAccess>]
type Args =
| Task of task: string
| Method of method: string
| Iterations of iterations: int
interface IArgParserTemplate with
member this.Usage =
match this with
| Task _ -> "Which task to perform. Options: Benchmark or Profile"
| Method _ -> "Which Method to profile. Options: V<number>. <number> = 01 - 10"
| Iterations _ -> "Number of iterations of the Method to perform for profiling"
[<EntryPoint>]
let main argv =
let parser = ArgumentParser.Create<Args> (programName = "Topological Sort")
let results = parser.Parse argv
let task = results.GetResult Args.Task
match task.ToLower() with
| "benchmark" ->
let _ = BenchmarkRunner.Run<Benchmarks>()
()
| "profile" ->
let method = results.GetResult Args.Method
let iterations = results.GetResult Args.Iterations
let _ = profile method iterations
()
| unknownTask -> failwith $"Unknown task: {unknownTask}"
1