-
-
Save khellang/04292b0d3be5e94878043e0db0e8f411 to your computer and use it in GitHub Desktop.
Xor combination of two Guids.
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
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; | |
} |
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 |
Yeah, that's what I expected ๐๐
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmm, the disassembly for XorPointer and XorSafe pretty much looks the same, difference might be alignment problem?
.NET Core 2.1.5 (CoreCLR 4.6.26919.02, CoreFX 4.6.26919.02), 64bit RyuJIT
.NET Core 2.1.5 (CoreCLR 4.6.26919.02, CoreFX 4.6.26919.02), 64bit RyuJIT
No ILOffsetMap found
System.Runtime.CompilerServices.Unsafe.As(!!0 ByRef)
.NET Core 2.1.5 (CoreCLR 4.6.26919.02, CoreFX 4.6.26919.02), 64bit RyuJIT