namespace Benchmarks; using System.Security.Cryptography; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Jobs; using BenchmarkDotNet.Order; [SimpleJob(RuntimeMoniker.Net80)] [SimpleJob(RuntimeMoniker.Net70)] [SimpleJob(RuntimeMoniker.Net60)] [Orderer(SummaryOrderPolicy.FastestToSlowest)] [RankColumn] [MemoryDiagnoser] [ThreadingDiagnoser] 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(this._data, this._offset, sizeof(short))); }