44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
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]
|
|
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)));
|
|
}
|