Skip to content

Instantly share code, notes, and snippets.

@marek-safar
Created January 13, 2020 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marek-safar/670c40f810cfd09b1498c52f1f8b6978 to your computer and use it in GitHub Desktop.
Save marek-safar/670c40f810cfd09b1498c52f1f8b6978 to your computer and use it in GitHub Desktop.
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];
}
}
@marek-safar
Copy link
Author

BenchmarkDotNet=v0.12.0, OS=macOS 10.15.2 (19C57) [Darwin 19.2.0]
Intel Core i7-7920HQ CPU 3.10GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=5.0.100-alpha.1.20061.5
  [Host]     : .NET Core 5.0.0 (CoreCLR 5.0.20.6301, CoreFX 4.700.19.56404), X64 RyuJIT
  DefaultJob : .NET Core 5.0.0 (CoreCLR 5.0.20.6007, CoreFX 5.0.20.6007), X64 RyuJIT


|           Method |                Input |     Mean |    Error |   StdDev |
|----------------- |--------------------- |---------:|---------:|---------:|
|    StringVersion | 18446744073709551615 | 96.21 ns | 1.507 ns | 1.336 ns |
| ConditionVersion | 18446744073709551615 | 87.36 ns | 0.485 ns | 0.430 ns |
|       ROSVersion | 18446744073709551615 | 99.03 ns | 1.316 ns | 1.231 ns |

@tannergooding
Copy link

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