Skip to content

Instantly share code, notes, and snippets.

@khellang
Forked from JimBobSquarePants/Xor.cs
Last active November 8, 2018 15:49
Show Gist options
  • Save khellang/04292b0d3be5e94878043e0db0e8f411 to your computer and use it in GitHub Desktop.
Save khellang/04292b0d3be5e94878043e0db0e8f411 to your computer and use it in GitHub Desktop.
Xor combination of two Guids.
public static Guid Xor(Guid a, Guid b)
{
var ad = new DecomposedGuid(a);
var bd = new DecomposedGuid(b);
ad.Hi ^= bd.Hi;
ad.Lo ^= bd.Lo;
return ad.Value;
}
[StructLayout(LayoutKind.Explicit)]
private struct DecomposedGuid
{
[FieldOffset(00)] public Guid Value;
[FieldOffset(00)] public long Hi;
[FieldOffset(08)] public long Lo;
public DecomposedGuid(Guid value) : this() => Value = value;
}
@Tornhoof
Copy link

Tornhoof commented Nov 8, 2018

Ok, the differences appear to be system related, something else is running on my system, after reboot it looks like:

BenchmarkDotNet=v0.11.2, OS=Windows 10.0.17134.376 (1803/April2018Update/Redstone4)
Intel Xeon CPU E5-1620 0 3.60GHz, 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=2.2.100-preview3-009430
  [Host]     : .NET Core 2.1.5 (CoreCLR 4.6.26919.02, CoreFX 4.6.26919.02), 64bit RyuJIT
  DefaultJob : .NET Core 2.1.5 (CoreCLR 4.6.26919.02, CoreFX 4.6.26919.02), 64bit RyuJIT

Method Mean Error StdDev
XorPointer 10.47 ns 0.3285 ns 0.3226 ns
XorUnsafe 14.37 ns 0.1059 ns 0.0939 ns
XorSafe 11.21 ns 0.1026 ns 0.0801 ns

@khellang
Copy link
Author

khellang commented Nov 8, 2018

Yeah, that's what I expected 😄👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment