continuity/Benchmarks/BinaryConversionBenchmarks.cs
Timothy Schenk 20931b5751
Some checks failed
Test if Server can be built / build-server (push) Failing after 11s
chore: use .net 8 for benchmarking and prematurely test .net 8
2023-10-27 19:27:10 +02:00

43 lines
1.1 KiB
C#

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<byte>(this.data, this.offset, sizeof(short)));
}