Last active
February 5, 2019 20:33
-
-
Save NullEntity/ebbeb1d6c58be1989323753283d3a93e to your computer and use it in GitHub Desktop.
Hash Code Distribution Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace HashCodeDistributionTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
GetHashCodeBenchmark.Setup(); | |
Console.WriteLine(GetHashCodeBenchmark.Counter.EqualsCount); | |
Console.WriteLine(GetHashCodeBenchmark.EvenDistributed.EqualsCount); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Counter: 0 | |
Evenly Distributed: 16724 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment