-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBitReader_ReadInt_Benchmark.cs
More file actions
110 lines (88 loc) · 2.8 KB
/
BitReader_ReadInt_Benchmark.cs
File metadata and controls
110 lines (88 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using BenchmarkDotNet.Attributes;
using NetStack.Serialization;
namespace NetCode.Benchmarks;
/// <summary>
/// BenchmarkDotNet=v0.13.1, OS=macOS Big Sur 11.5.2 (20G95) [Darwin 20.6.0]
/// Intel Core i7-9750H CPU 2.60GHz, 1 CPU, 12 logical and 6 physical cores
/// .NET SDK=6.0.100
/// [Host] : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT
/// DefaultJob : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT
///
/// | Method | Mean | Error | StdDev | Ratio | RatioSD |
/// |---------------------------- |-----------:|---------:|---------:|------:|--------:|
/// | BitReader_Aligned_ReadInt | 518.2 ns | 2.62 ns | 2.45 ns | 1.00 | 0.00 |
/// | BitReader_UnAligned_ReadInt | 808.9 ns | 2.35 ns | 2.08 ns | 1.56 | 0.01 |
/// | BitBuffer_Aligned_ReadInt | 2,403.0 ns | 14.81 ns | 13.85 ns | 4.64 | 0.04 |
/// | BitBuffer_UnAligned_ReadInt | 2,899.6 ns | 22.16 ns | 19.64 ns | 5.60 | 0.04 |
///
/// </summary>
public class BitReader_ReadInt_Benchmark
{
private const int ReadCount = 255;
private const int BitsPerRead = 32;
private BitReader _bitReader;
private byte[] _array;
private BitBuffer _bitBuffer;
[GlobalSetup]
public void GlobalSetup()
{
var arrayLength = (int)Math.Ceiling((float) ReadCount * BitsPerRead / 8) + 1;
_array = new byte[arrayLength]; // 1021
for (int i = 0; i < _array.Length; i++)
{
_array[i] = (byte)i;
}
_bitReader = new BitReader();
_bitBuffer = new BitBuffer();
}
[Benchmark(Baseline = true)]
public int BitReader_Aligned_ReadInt()
{
var s = 0;
_bitReader.SetArray(_array);
for (int i = 0; i < ReadCount; i++)
{
var value = _bitReader.ReadInt();
s += value;
}
return s;
}
[Benchmark]
public int BitReader_UnAligned_ReadInt()
{
var s = 0;
_bitReader.SetArray(_array);
_bitReader.ReadBits(1);
for (int i = 0; i < ReadCount; i++)
{
var value = _bitReader.ReadInt();
s += value;
}
return s;
}
[Benchmark]
public int BitBuffer_Aligned_ReadInt()
{
var s = 0;
_bitBuffer.FromArray(_array, _array.Length);
for (int i = 0; i < ReadCount; i++)
{
var value = _bitBuffer.ReadInt();
s += value;
}
return s;
}
[Benchmark]
public int BitBuffer_UnAligned_ReadInt()
{
var s = 0;
_bitBuffer.FromArray(_array, _array.Length);
_bitBuffer.Read(1);
for (int i = 0; i < ReadCount; i++)
{
var value = _bitBuffer.ReadInt();
s += value;
}
return s;
}
}