37 lines
1 KiB
C#
37 lines
1 KiB
C#
namespace Benchmarks;
|
|
|
|
using System.Security.Cryptography;
|
|
using BenchmarkDotNet.Attributes;
|
|
using BenchmarkDotNet.Order;
|
|
|
|
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
|
|
[Config(typeof(GenericConfig))]
|
|
public class BinaryConversionBenchmarks
|
|
{
|
|
private byte[] _data = null!;
|
|
private int _offset;
|
|
|
|
[GlobalSetup]
|
|
public void Setup()
|
|
{
|
|
this._data = RandomNumberGenerator.GetBytes(4000);
|
|
this._offset = RandomNumberGenerator.GetInt32(0, 3500);
|
|
}
|
|
|
|
[Benchmark]
|
|
public short BitConverterTest() => BitConverter.ToInt16(this._data, this._offset);
|
|
|
|
[Benchmark]
|
|
public short BinaryReader()
|
|
{
|
|
using var ms = new MemoryStream(this._data);
|
|
using var reader = new BinaryReader(ms);
|
|
reader.BaseStream.Position = this._offset;
|
|
return reader.ReadInt16();
|
|
}
|
|
|
|
[Benchmark]
|
|
public short BinaryPrimitives() =>
|
|
System.Buffers.Binary.BinaryPrimitives.ReadInt16LittleEndian(
|
|
new ArraySegment<byte>(this._data, this._offset, sizeof(short)));
|
|
}
|