2021-09-27 12:42:46 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Data.HashFunction.xxHash;
|
|
|
|
|
using BenchmarkDotNet.Attributes;
|
|
|
|
|
using BenchmarkDotNet.Running;
|
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
namespace Wabbajack.Hashing.xxHash64.Benchmark;
|
|
|
|
|
|
|
|
|
|
internal class Program
|
2021-09-27 12:42:46 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
private static void Main(string[] args)
|
2021-09-27 12:42:46 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
BenchmarkRunner.Run<Base64EncoderBenchmark>();
|
2021-09-27 12:42:46 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
[MemoryDiagnoser]
|
|
|
|
|
[DisassemblyDiagnoser(3)]
|
|
|
|
|
public class xxHashBenchmark
|
|
|
|
|
{
|
|
|
|
|
private readonly byte[] _data;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public xxHashBenchmark()
|
|
|
|
|
{
|
|
|
|
|
_data = new byte[1024 * 1024];
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
[Benchmark]
|
|
|
|
|
public void NewCode()
|
|
|
|
|
{
|
|
|
|
|
var hash = new xxHashAlgorithm(0);
|
|
|
|
|
hash.HashBytes(_data);
|
2021-09-27 12:42:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
[Benchmark]
|
|
|
|
|
public void OldCode()
|
2021-09-27 12:42:46 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
var config = new xxHashConfig {HashSizeInBits = 64};
|
|
|
|
|
BitConverter.ToUInt64(xxHashFactory.Instance.Create(config).ComputeHash(_data).Hash);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
[MemoryDiagnoser]
|
|
|
|
|
[DisassemblyDiagnoser(3)]
|
|
|
|
|
public class Base64EncoderBenchmark
|
|
|
|
|
{
|
|
|
|
|
private readonly Hash _data;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public Base64EncoderBenchmark()
|
|
|
|
|
{
|
|
|
|
|
_data = new Hash(ulong.MaxValue >> 4);
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
[Benchmark]
|
|
|
|
|
public void NewCode()
|
|
|
|
|
{
|
|
|
|
|
unsafe
|
2021-09-27 12:42:46 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
Span<byte> buffer = stackalloc byte[12];
|
|
|
|
|
_data.ToBase64(buffer);
|
|
|
|
|
var result = Hash.FromBase64(buffer);
|
2021-09-27 12:42:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
|
|
|
|
|
[Benchmark]
|
|
|
|
|
public void OldCode()
|
|
|
|
|
{
|
|
|
|
|
var result = Hash.FromBase64(_data.ToBase64());
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
}
|