Skip to content

Instantly share code, notes, and snippets.

@NullEntity
Last active February 5, 2019 20:33
Show Gist options
  • Save NullEntity/ebbeb1d6c58be1989323753283d3a93e to your computer and use it in GitHub Desktop.
Save NullEntity/ebbeb1d6c58be1989323753283d3a93e to your computer and use it in GitHub Desktop.
Hash Code Distribution Test
using System.Collections;
namespace HashCodeDistributionTest
{
public class GetHashCodeBenchmark
{
private static readonly Hashtable _evenDistributes = new Hashtable();
private static readonly Hashtable _counters = new Hashtable();
public class EvenDistributed
{
public static int EqualsCount = 0;
public override int GetHashCode() => base.GetHashCode();
public override bool Equals(object obj)
{
EqualsCount++;
return false;
}
}
public class Counter
{
public static int EqualsCount = 0;
private static int _lastHash;
public override int GetHashCode() => _lastHash++;
public override bool Equals(object obj)
{
EqualsCount++;
return false;
}
}
public static void Setup()
{
for (var i = 0; i < 1_500_000; i++)
{
_evenDistributes[new EvenDistributed()] = 0;
_counters[new Counter()] = 0;
}
}
}
}
using System;
namespace HashCodeDistributionTest
{
class Program
{
static void Main(string[] args)
{
GetHashCodeBenchmark.Setup();
Console.WriteLine(GetHashCodeBenchmark.Counter.EqualsCount);
Console.WriteLine(GetHashCodeBenchmark.EvenDistributed.EqualsCount);
}
}
}
Counter: 0
Evenly Distributed: 16724
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment