feat: add benchmark for writing to span/array of bytes
All checks were successful
Build, Package and Push Images / preprocess (push) Successful in 2s
Build, Package and Push Images / build (push) Successful in 27s
Build, Package and Push Images / sonarqube (push) Has been skipped
Build, Package and Push Images / sbom-scan (push) Successful in 31s
Build, Package and Push Images / container-build (push) Successful in 1m22s
Build, Package and Push Images / container-sbom-scan (push) Successful in 37s

This commit is contained in:
Timothy Schenk 2023-11-13 19:32:47 +01:00
parent 9a827214fa
commit 293b65a856
2 changed files with 27 additions and 9 deletions

View file

@ -10,16 +10,18 @@ public class BinaryConversionBenchmarks
{ {
private byte[] _data = null!; private byte[] _data = null!;
private int _offset; private int _offset;
private int _writeBuffer;
[GlobalSetup] [GlobalSetup]
public void Setup() public void Setup()
{ {
this._data = RandomNumberGenerator.GetBytes(4000); this._data = RandomNumberGenerator.GetBytes(4000);
this._offset = RandomNumberGenerator.GetInt32(0, 3500); this._offset = RandomNumberGenerator.GetInt32(0, 3500);
this._writeBuffer = RandomNumberGenerator.GetInt32(int.MinValue, int.MaxValue);
} }
[Benchmark] [Benchmark]
public short BitConverterTest() => BitConverter.ToInt16(this._data, this._offset); public short BitConverterParseTest() => BitConverter.ToInt16(this._data, this._offset);
[Benchmark] [Benchmark]
public short BinaryReader() public short BinaryReader()
@ -31,7 +33,30 @@ public class BinaryConversionBenchmarks
} }
[Benchmark] [Benchmark]
public short BinaryPrimitives() => public short BinaryPrimitivesRead() =>
System.Buffers.Binary.BinaryPrimitives.ReadInt16LittleEndian( System.Buffers.Binary.BinaryPrimitives.ReadInt16LittleEndian(
new ArraySegment<byte>(this._data, this._offset, sizeof(short))); new ArraySegment<byte>(this._data, this._offset, sizeof(short)));
[Benchmark]
public void BinaryPrimitivesWrite()
{
System.Buffers.Binary.BinaryPrimitives.WriteInt32LittleEndian(this._data.AsSpan(_offset, 4),
this._writeBuffer);
}
[Benchmark]
public void BitConverterCopy()
{
BitConverter.GetBytes(this._writeBuffer).CopyTo(this._data, this._offset);
}
[Benchmark]
public void BitConverterAssignment()
{
var bytes = BitConverter.GetBytes(this._writeBuffer);
this._data[this._offset] = bytes[0];
this._data[this._offset + 1] = bytes[1];
this._data[this._offset + 2] = bytes[2];
this._data[this._offset + 3] = bytes[3];
}
} }

View file

@ -27,12 +27,5 @@ public class GenericConfig : ManualConfig
.AddAnalyser(MinIterationTimeAnalyser.Default, OutliersAnalyser.Default, .AddAnalyser(MinIterationTimeAnalyser.Default, OutliersAnalyser.Default,
RuntimeErrorAnalyser.Default, EnvironmentAnalyser.Default) RuntimeErrorAnalyser.Default, EnvironmentAnalyser.Default)
.AddColumn(RankColumn.Arabic).AddExporter(CsvExporter.Default, MarkdownExporter.Default); .AddColumn(RankColumn.Arabic).AddExporter(CsvExporter.Default, MarkdownExporter.Default);
AddJob(Job.Default
.WithRuntime(CoreRuntime.Core60))
.AddDiagnoser(ThreadingDiagnoser.Default, MemoryDiagnoser.Default,
EventPipeProfiler.Default)
.AddAnalyser(MinIterationTimeAnalyser.Default, OutliersAnalyser.Default,
RuntimeErrorAnalyser.Default, EnvironmentAnalyser.Default)
.AddColumn(RankColumn.Arabic).AddExporter(CsvExporter.Default, MarkdownExporter.Default);
} }
} }