Skip to content

Instantly share code, notes, and snippets.

@i3arnon
Last active September 21, 2017 16:15
Show Gist options
  • Save i3arnon/e9f85402790fcbd1d14450540f7ec496 to your computer and use it in GitHub Desktop.
Save i3arnon/e9f85402790fcbd1d14450540f7ec496 to your computer and use it in GitHub Desktop.
[MemoryDiagnoser]
public class Program
{
private const int NumberOfLabels = IPAddressParserStatics.IPv6AddressBytes / 2;
private static readonly uint ScopeId;
private static readonly ushort[] Numbers;
static Program()
{
var ipAddress = IPAddress.Parse("2001:4860:4860::8888");
var bytes = ipAddress.GetAddressBytes();
ScopeId = (uint)ipAddress.ScopeId;
Numbers = new ushort[NumberOfLabels];
for (var i = 0; i < NumberOfLabels; i ++)
{
Numbers[i] = (ushort)(bytes[i * 2] << sizeof(byte) | bytes[i * 2 + 1]);
}
}
public static void Main() => BenchmarkRunner.Run<Program>();
[Benchmark]
public int GetHashCodeToString() =>
StringComparer.OrdinalIgnoreCase.GetHashCode(IPAddressParser.IPv6AddressToString(Numbers, ScopeId));
[Benchmark]
public int GetHashCodeHashHelpers()
{
var hashCode = HashHelpers.Combine(HashHelpers.RandomSeed, unchecked((int)ScopeId));
for (int i = 0; i < NumberOfLabels; i += 2)
{
hashCode = HashHelpers.Combine(hashCode, Numbers[i] << 16 | Numbers[i + 1]);
}
return hashCode;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment