.net - Fastest way to check the numerical value of a boxed primitive integral type in C# -
i need write method following semantics:
/// <summary> /// checks if <paramref name="x"/> boxed instance of primitive integral type /// numerical value equals <paramref name="y"/>. /// </summary> /// <param name="x">an object reference. can <c>null</c>.</param> /// <param name="y">a numerical value of type <see cref="ulong"/> compare with.</param> /// <returns> /// <c>true</c> if <paramref name="x"/> refers boxed instance of type /// <see cref="sbyte"/>, <see cref="short"/>, <see cref="int"/>, <see cref="long"/>, /// <see cref="byte"/>, <see cref="ushort"/>, <see cref="uint"/>, or <see cref="ulong"/>, /// numerical value equals numerical value of <paramref name="y"/>; otherwise, <c>false</c>. /// </returns> /// <remarks> /// <para> /// method checks numeric equality, if arguments of different runtime types /// (e.g. <c>2l</c> considered equal <c>2ul</c>). /// </para> /// <para> /// method returns <c>false</c> if <paramref name="x"/> <c>null</c> /// or refers instance of reference type or boxed instance of value type except /// primitive integral types listed above (e.g. returns <c>false</c> if <paramref name="x"/> /// refers boxed instance of <c>enum</c> type, <see cref="bool"/>, <see cref="char"/>, <see cref="intptr"/>, /// <see cref="uintptr"/>, <see cref="float"/>, <see cref="double"/>, <see cref="decimal"/>, or <see cref="biginteger"/>). /// </para> /// <para> /// method should not throw exceptions, or cause observable side-effects /// (e.g. invoke method modify state of object referenced <paramref name="x"/>). /// </para> /// </remarks> [pure] public static bool numericalequals(object x, ulong y)
the implementation should fast possible (assuming there no expected bias in input data towards types or values of parameter x
), , should not use unsafe
code or p/invoke. of course, among fastest implementations, prefer 1 simplest , shortest.
my solution follows:
public static bool numericalequals(object x, ulong y) { if (x sbyte) { sbyte z = (sbyte)x; return z >= 0 && y == (ulong)z; } if (x short) { short z = (short)x; return z >= 0 && y == (ulong)z; } if (x int) { int z = (int)x; return z >= 0 && y == (ulong)z; } if (x long) { long z = (long)x; return z >= 0 && y == (ulong)z; } if (x byte) { return y == (byte)x; } if (x ushort) { return y == (ushort)x; } if (x uint) { return y == (uint)x; } if (x ulong) { return y == (ulong)x; } return false; }
could suggest better approach?
since each of integer primitive types sealed, is
operations performed reference implementation compiled jit equality comparison between runtimetypehandle
of type of object x
(likely obtained through either 1 or 2 pointer dereferences) , handle of particular integer type (likely implemented inline literal or single mov
instruction references fixed memory location). difficult, if not impossible, improve upon initial implementation without leveraging information distribution of inputs method.
Comments
Post a Comment