Created
January 13, 2020 22:20
-
-
Save marek-safar/670c40f810cfd09b1498c52f1f8b6978 to your computer and use it in GitHub Desktop.
This file contains 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 class Benchmarks | |
{ | |
static byte[] array = new byte[32]; | |
static int res; | |
[Params(ulong.MaxValue)] | |
public ulong Input { get; set; } | |
[Benchmark] | |
public bool StringVersion() | |
{ | |
return Best.StringVersion(Input, ref res); | |
} | |
[Benchmark] | |
public bool ConditionVersion() | |
{ | |
return Best.ConditionVersion(Input, ref res); | |
} | |
[Benchmark] | |
public bool ROSVersion() | |
{ | |
return Best.ROSVersion(Input, ref res); | |
} | |
} | |
static class Best | |
{ | |
public static bool ConditionVersion(ulong value, ref int res) | |
{ | |
int computedOutputLength = 70; | |
while (--computedOutputLength != 0) | |
{ | |
res += (byte)Convert_Condition((int)value); | |
value >>= 1; | |
} | |
return true; | |
} | |
public static bool StringVersion(ulong value, ref int res) | |
{ | |
int computedOutputLength = 70; | |
while (--computedOutputLength != 0) | |
{ | |
res += (byte)Convert_String((int)value); | |
value >>= 1; | |
} | |
return true; | |
} | |
public static bool ROSVersion(ulong value, ref int res) | |
{ | |
int computedOutputLength = 70; | |
while (--computedOutputLength != 0) | |
{ | |
res += (byte)Convert_ROS((int)value); | |
value >>= 1; | |
} | |
return true; | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static char Convert_Condition (int value) | |
{ | |
value &= 0xF; | |
return (char)(value > 9 ? value - 10 + 'A' : value + '0'); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static char Convert_String (int value) | |
{ | |
return "0123456789ABCDEF"[value & 0xF]; | |
} | |
internal static ReadOnlySpan<byte> ros => new byte[16] | |
{ | |
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', | |
(byte)'8', (byte)'9', (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F' | |
}; | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static char Convert_ROS (int value) | |
{ | |
return (char)ros[value & 0xF]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a counter point:
This can depend greatly on the CPU you are running against, the input payload, and a number of other factors