Skip to content

Instantly share code, notes, and snippets.

@VisualMelon
Created October 24, 2020 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VisualMelon/00885fe50f7ab0f4ae5cd1307312109f to your computer and use it in GitHub Desktop.
Save VisualMelon/00885fe50f7ab0f4ae5cd1307312109f to your computer and use it in GitHub Desktop.
``` ini
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19041.572 (2004/?/20H1)
AMD Ryzen 7 3700X, 1 CPU, 16 logical and 8 physical cores
.NET Core SDK=5.0.100-preview.8.20417.9
[Host] : .NET Core 5.0.0 (CoreCLR 5.0.20.40711, CoreFX 5.0.20.40711), X64 RyuJIT
DefaultJob : .NET Core 5.0.0 (CoreCLR 5.0.20.40711, CoreFX 5.0.20.40711), X64 RyuJIT
```
| Method | Size | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|-------------- |------ |-------------:|------------:|------------:|------:|--------:|---------:|---------:|---------:|----------:|
| **LinqSort** | **30** | **1.078 μs** | **0.0076 μs** | **0.0071 μs** | **1.00** | **0.00** | **0.0801** | **-** | **-** | **672 B** |
| PriorityQueue | 30 | 1.048 μs | 0.0042 μs | 0.0039 μs | 0.97 | 0.01 | - | - | - | - |
| PrioritySet | 30 | 1.991 μs | 0.0031 μs | 0.0027 μs | 1.85 | 0.01 | - | - | - | - |
| PairingHeap | 30 | 2.513 μs | 0.0250 μs | 0.0234 μs | 2.33 | 0.03 | 0.1717 | - | - | 1440 B |
| | | | | | | | | | | |
| **LinqSort** | **300** | **18.134 μs** | **0.1345 μs** | **0.1192 μs** | **1.00** | **0.00** | **0.4578** | **-** | **-** | **3912 B** |
| PriorityQueue | 300 | 22.106 μs | 0.2222 μs | 0.2078 μs | 1.22 | 0.01 | - | - | - | - |
| PrioritySet | 300 | 34.303 μs | 0.0602 μs | 0.0533 μs | 1.89 | 0.01 | - | - | - | - |
| PairingHeap | 300 | 47.776 μs | 0.5092 μs | 0.4763 μs | 2.64 | 0.03 | 1.7090 | 0.0610 | - | 14400 B |
| | | | | | | | | | | |
| **LinqSort** | **3000** | **306.185 μs** | **0.7864 μs** | **0.6972 μs** | **1.00** | **0.00** | **3.9063** | **-** | **-** | **36312 B** |
| PriorityQueue | 3000 | 350.406 μs | 0.8858 μs | 0.7853 μs | 1.14 | 0.00 | - | - | - | - |
| PrioritySet | 3000 | 490.053 μs | 2.1830 μs | 2.0420 μs | 1.60 | 0.01 | - | - | - | - |
| PairingHeap | 3000 | 635.979 μs | 5.1801 μs | 4.8455 μs | 2.08 | 0.02 | 16.6016 | 3.9063 | - | 144000 B |
| | | | | | | | | | | |
| **LinqSort** | **30000** | **3,845.923 μs** | **6.6054 μs** | **6.1787 μs** | **1.00** | **0.00** | **101.5625** | **101.5625** | **101.5625** | **360973 B** |
| PriorityQueue | 30000 | 4,165.286 μs | 52.2600 μs | 48.8841 μs | 1.08 | 0.01 | - | - | - | - |
| PrioritySet | 30000 | 6,432.610 μs | 49.4819 μs | 46.2854 μs | 1.67 | 0.01 | - | - | - | - |
| PairingHeap | 30000 | 7,923.529 μs | 107.1215 μs | 100.2015 μs | 2.06 | 0.03 | 171.8750 | 78.1250 | - | 1440000 B |
using BenchmarkDotNet.Attributes;
using System;
using System.Linq;
using AlgoKit.Collections.Heaps;
using System.Collections.Generic;
namespace PriorityQueue.Benchmarks
{
[MemoryDiagnoser]
public class HeapSortBenchmarks
{
[Params(30, 300, 3000, 30_000)]
public int Size;
private int[] _priorities;
private PriorityQueue<int, int> _priorityQueue;
private PrioritySet<int, int> _prioritySet;
private PairingHeap<int, int> _pairingHeap;
[GlobalSetup]
public void Initialize()
{
var random = new Random(42);
_priorities = new int[Size];
for (int i = 0; i < Size; i++)
{
_priorities[i] = random.Next(20);
}
_priorityQueue = new PriorityQueue<int, int>(initialCapacity: Size);
_prioritySet = new PrioritySet<int, int>(initialCapacity: Size);
_pairingHeap = new PairingHeap<int, int>(Comparer<int>.Default);
}
[Benchmark(Baseline = true)]
public void LinqSort()
{
foreach (int priority in _priorities.OrderBy(x => x))
{
}
}
[Benchmark]
public void PriorityQueue()
{
var queue = _priorityQueue;
var priorities = _priorities;
for (int i = 0; i < Size; i++)
{
queue.Enqueue(i, priorities[i]);
}
while (queue.Count > 0)
{
queue.Dequeue();
}
}
[Benchmark]
public void PrioritySet()
{
var queue = _prioritySet;
var priorities = _priorities;
for (int i = 0; i < Size; i++)
{
queue.Enqueue(i, priorities[i]);
}
while (queue.Count > 0)
{
queue.Dequeue();
}
}
[Benchmark]
public void PairingHeap()
{
var queue = _pairingHeap;
var priorities = _priorities;
for (int i = 0; i < Size; i++)
{
queue.Add(priorities[i], i);
}
while (queue.Count > 0)
{
queue.Pop();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment