-
Notifications
You must be signed in to change notification settings - Fork 1
/
UpdateBenchmarks.cs
105 lines (90 loc) · 2.87 KB
/
UpdateBenchmarks.cs
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
using BenchmarkDotNet.Attributes;
using System;
using System.Linq;
using System.Collections.Generic;
using AlgoKit.Collections.Heaps;
namespace PriorityQueue.Benchmarks
{
[MemoryDiagnoser]
public class UpdateBenchmarks
{
[Params(10, 50, 150, 500, 1000, 10_000, 1_000_000)]
public int Size;
private int[] _priorities;
private PrioritySet<int, int> _prioritySet;
private PairingHeap<int, int> _pairingHeap;
private Dictionary<int, PairingHeapNode<int, int>> _pairingHeapNodes;
[GlobalSetup]
public void Initialize()
{
var random = new Random(42);
_priorities = new int[Size];
for (int i = 0; i < Size; i++)
{
_priorities[i] = random.Next();
}
_prioritySet = new PrioritySet<int, int>(initialCapacity: Size);
_pairingHeap = new PairingHeap<int, int>(Comparer<int>.Default);
_pairingHeapNodes = new Dictionary<int, PairingHeapNode<int, int>>(Size);
}
// [IterationSetup]
// public void IterSetup()
// {
// var prioritySet = _prioritySet;
// var priorities = _priorities;
// var pairingHeap = _pairingHeap;
// var pairingHeapNodes = _pairingHeapNodes;
// for (int i = 0; i < Size; i++)
// {
// prioritySet.Enqueue(i, priorities[i]);
// PairingHeapNode<int, int> node = pairingHeap.Add(priorities[i], i);
// pairingHeapNodes.Add(i, node);
// }
// }
// [IterationCleanup]
// public void IterCleanup()
// {
// _prioritySet.Clear();
// _pairingHeap.Clear();
// _pairingHeapNodes.Clear();
// }
[Benchmark]
public void PrioritySet()
{
var queue = _prioritySet;
var priorities = _priorities;
for (int i = 0; i < Size; i++)
{
queue.Enqueue(i, priorities[i]);
}
for (int i = 0; i < Size; i++)
{
queue.TryUpdate(i, -priorities[i]);
}
while (queue.Count > 0)
{
queue.Dequeue();
}
}
[Benchmark]
public void PairingHeap()
{
var heap = _pairingHeap;
var heapIndex = _pairingHeapNodes;
var priorities = _priorities;
for (int i = 0; i < Size; i++)
{
PairingHeapNode<int, int> node = heap.Add(priorities[i], i);
heapIndex[i] = node;
}
for (int i = 0; i < Size; i++)
{
heap.Update(heapIndex[i], -priorities[i]);
}
while (heap.Count > 0)
{
heap.Pop();
}
}
}
}