Skip to content

Instantly share code, notes, and snippets.

@jskeet
Last active November 23, 2023 17:54
Show Gist options
  • Save jskeet/3686721a9736982653f1e9b2a1e8ee75 to your computer and use it in GitHub Desktop.
Save jskeet/3686721a9736982653f1e9b2a1e8ee75 to your computer and use it in GitHub Desktop.
using System.Diagnostics;
const int CountPerInnerLoop = 10_000_000;
const int Iterations = 5;
for (int iteration = 1; iteration <= Iterations; iteration++)
{
Console.WriteLine($"Iteration {iteration}");
var list = new List<int>();
for (int i = 0; i <= 10000; i++)
{
list.Add(3);
}
var list2 = new List<int>();
for (int i = 0; i <= 10000; i++)
{
list2.Add(i);
}
var watch = new Stopwatch();
watch.Start();
for (int i = 0; i < CountPerInnerLoop; i++)
{
list.Contains(10000);
}
watch.Stop();
Console.WriteLine(watch.Elapsed.Ticks);
var hash = list.ToHashSet();
watch = new Stopwatch();
watch.Start();
for (int i = 0; i < CountPerInnerLoop; i++)
{
hash.Contains(10000);
}
watch.Stop();
Console.WriteLine(watch.Elapsed.Ticks);
watch = new Stopwatch();
watch.Start();
for (int i = 0; i < CountPerInnerLoop; i++)
{
list2.Contains(10000);
}
Console.WriteLine(watch.Elapsed.Ticks);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment