diff --git a/Benchmarks/Benchmarks.csproj b/Benchmarks/Benchmarks.csproj new file mode 100644 index 0000000..3d08c42 --- /dev/null +++ b/Benchmarks/Benchmarks.csproj @@ -0,0 +1,15 @@ + + + + Exe + enable + enable + default + net7.0 + + + + + + + diff --git a/Benchmarks/BinaryConversionBenchmarks.cs b/Benchmarks/BinaryConversionBenchmarks.cs new file mode 100644 index 0000000..ee9961d --- /dev/null +++ b/Benchmarks/BinaryConversionBenchmarks.cs @@ -0,0 +1,50 @@ +using System.Security.Cryptography; +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Jobs; +using BenchmarkDotNet.Order; + +namespace Benchmarks; + +[SimpleJob(RuntimeMoniker.NativeAot70)] +[SimpleJob(RuntimeMoniker.Net70)] +[SimpleJob(RuntimeMoniker.Net60)] +[Orderer(SummaryOrderPolicy.FastestToSlowest)] +[RankColumn] +[MemoryDiagnoser] +[ExceptionDiagnoser] +[ThreadingDiagnoser] +[DisassemblyDiagnoser] +public class BinaryConversionBenchmarks +{ + private byte[] data; + private int offset; + + [GlobalSetup] + public void Setup() + { + data = RandomNumberGenerator.GetBytes(4000); + offset = RandomNumberGenerator.GetInt32(0, 3500); + } + + [Benchmark] + public Int16 BitConverterTest() + { + return BitConverter.ToInt16(data, offset); + } + + [Benchmark] + public Int16 BinaryReader() + { + using MemoryStream ms = new MemoryStream(data); + using BinaryReader reader = new BinaryReader(ms); + reader.BaseStream.Position = offset; + return reader.ReadInt16(); + } + + [Benchmark] + public Int16 BinaryPrimitives() + { + return System.Buffers.Binary.BinaryPrimitives.ReadInt16LittleEndian( + new ArraySegment(data, offset, sizeof(short))); + } +} \ No newline at end of file diff --git a/Benchmarks/Program.cs b/Benchmarks/Program.cs new file mode 100644 index 0000000..2e87545 --- /dev/null +++ b/Benchmarks/Program.cs @@ -0,0 +1,12 @@ +using System.Reflection; +using BenchmarkDotNet.Running; + +namespace Benchmarks; + +internal class Program +{ + public static void Main(string[] args) + { + BenchmarkRunner.Run(Assembly.GetExecutingAssembly()); + } +} \ No newline at end of file