2024-02-07 16:40:36 +01:00
|
|
|
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
|
2023-11-20 19:58:30 +01:00
|
|
|
|
2023-11-19 17:07:28 +01:00
|
|
|
using System.Buffers.Binary;
|
2023-08-12 23:02:59 +02:00
|
|
|
using System.Security.Cryptography;
|
2023-08-10 22:07:40 +02:00
|
|
|
using BenchmarkDotNet.Attributes;
|
|
|
|
using BenchmarkDotNet.Order;
|
|
|
|
|
2023-11-19 17:07:28 +01:00
|
|
|
namespace Benchmarks;
|
|
|
|
|
2023-08-10 22:07:40 +02:00
|
|
|
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
|
2023-11-08 15:34:09 +01:00
|
|
|
[Config(typeof(GenericConfig))]
|
2025-01-16 14:30:40 +01:00
|
|
|
public class BinaryConversionBenchmarks {
|
2023-10-27 19:47:17 +02:00
|
|
|
private byte[] _data = null!;
|
|
|
|
private int _offset;
|
2023-11-13 19:32:47 +01:00
|
|
|
private int _writeBuffer;
|
2023-08-10 22:07:40 +02:00
|
|
|
|
|
|
|
[GlobalSetup]
|
2025-01-16 14:30:40 +01:00
|
|
|
public void Setup() {
|
2023-11-19 17:07:28 +01:00
|
|
|
_data = RandomNumberGenerator.GetBytes(4000);
|
|
|
|
_offset = RandomNumberGenerator.GetInt32(0, 3500);
|
|
|
|
_writeBuffer = RandomNumberGenerator.GetInt32(int.MinValue, int.MaxValue);
|
2023-08-10 22:07:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[Benchmark]
|
2025-01-16 14:30:40 +01:00
|
|
|
public short BitConverterParseTest() {
|
2023-11-19 17:07:28 +01:00
|
|
|
return BitConverter.ToInt16(_data, _offset);
|
|
|
|
}
|
2023-08-10 22:07:40 +02:00
|
|
|
|
|
|
|
[Benchmark]
|
2025-01-16 14:30:40 +01:00
|
|
|
public short BinaryReader() {
|
2023-11-19 17:07:28 +01:00
|
|
|
using var ms = new MemoryStream(_data);
|
2023-08-11 11:31:30 +02:00
|
|
|
using var reader = new BinaryReader(ms);
|
2023-11-19 17:07:28 +01:00
|
|
|
reader.BaseStream.Position = _offset;
|
2023-08-10 22:07:40 +02:00
|
|
|
return reader.ReadInt16();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Benchmark]
|
2025-01-16 14:30:40 +01:00
|
|
|
public short BinaryPrimitivesRead() {
|
2023-11-19 17:07:28 +01:00
|
|
|
return BinaryPrimitives.ReadInt16LittleEndian(
|
|
|
|
new ArraySegment<byte>(_data, _offset, sizeof(short)));
|
|
|
|
}
|
2023-11-13 19:32:47 +01:00
|
|
|
|
|
|
|
[Benchmark]
|
2025-01-16 14:30:40 +01:00
|
|
|
public void BinaryPrimitivesWrite() {
|
2023-11-19 17:07:28 +01:00
|
|
|
BinaryPrimitives.WriteInt32LittleEndian(_data.AsSpan(_offset, 4),
|
|
|
|
_writeBuffer);
|
2023-11-13 19:32:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[Benchmark]
|
2025-01-16 14:30:40 +01:00
|
|
|
public void BitConverterCopy() {
|
2023-11-19 17:07:28 +01:00
|
|
|
BitConverter.GetBytes(_writeBuffer).CopyTo(_data, _offset);
|
2023-11-13 19:32:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[Benchmark]
|
2025-01-16 14:30:40 +01:00
|
|
|
public void BitConverterAssignment() {
|
2023-11-19 17:07:28 +01:00
|
|
|
var bytes = BitConverter.GetBytes(_writeBuffer);
|
|
|
|
_data[_offset] = bytes[0];
|
|
|
|
_data[_offset + 1] = bytes[1];
|
|
|
|
_data[_offset + 2] = bytes[2];
|
|
|
|
_data[_offset + 3] = bytes[3];
|
2023-11-13 19:32:47 +01:00
|
|
|
}
|
2023-08-11 11:31:30 +02:00
|
|
|
}
|