Skip to content

Instantly share code, notes, and snippets.

@IJzerbaard
Created November 4, 2018 18:00
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 IJzerbaard/bf8711c5dc59dcf096681633d1c8dfbb to your computer and use it in GitHub Desktop.
Save IJzerbaard/bf8711c5dc59dcf096681633d1c8dfbb to your computer and use it in GitHub Desktop.
Benchmark sortedset versus a MinHeap implementation on a load that a heap might see.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace CSTest
{
class Program
{
static void Main(string[] args)
{
for (int j = 0; j < 10; j++)
{
bool b = (j & 1) == 1;
var sw = Stopwatch.StartNew();
if (b)
{
MinHeap<int> minHeap = new MinHeap<int>();
for (int i = 0; i < 1000; i++)
{
int r = (int)(i * 0xDEADBEEF);
minHeap.Push(r);
}
for (int i = 0; i < 100000; i++)
{
int r = (int)(i * 0xDEADBEEF >> 24);
if ((r & 1) == 0)
minHeap.Push(r);
else
minHeap.Pop();
}
while (!minHeap.Empty)
minHeap.Pop();
}
else
{
SortedSet<int> ss = new SortedSet<int>();
for (int i = 0; i < 1000; i++)
{
int r = (int)(i * 0xDEADBEEF);
ss.Add(r);
}
for (int i = 0; i < 100000; i++)
{
int r = (int)(i * 0xDEADBEEF >> 24);
if ((r & 1) == 0)
ss.Add(r);
else
ss.Remove(ss.Min);
}
while (ss.Count != 0)
ss.Remove(ss.Min);
}
sw.Stop();
Console.WriteLine("{0}: {1}", b ? "heap" : "sset", sw.ElapsedTicks);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment