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]; | |
} | |
} |
Author
marek-safar
commented
Jan 13, 2020
As a counter point:
BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
AMD Ryzen 9 3900X, 1 CPU, 24 logical and 12 physical cores
.NET Core SDK=5.0.100-alpha.1.20064.2
[Host] : .NET Core 5.0.0 (CoreCLR 5.0.20.6307, CoreFX 5.0.20.6307), X64 RyuJIT [AttachedDebugger]
DefaultJob : .NET Core 5.0.0 (CoreCLR 5.0.20.6307, CoreFX 5.0.20.6307), X64 RyuJIT
| Method | Input | Mean | Error | StdDev |
|----------------- |--------------------- |---------:|---------:|---------:|
| StringVersion | 18446744073709551615 | 44.64 ns | 0.235 ns | 0.220 ns |
| ConditionVersion | 18446744073709551615 | 58.82 ns | 1.199 ns | 1.516 ns |
| ROSVersion | 18446744073709551615 | 43.57 ns | 0.291 ns | 0.227 ns |
This can depend greatly on the CPU you are running against, the input payload, and a number of other factors
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment