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; 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))); }