-
Notifications
You must be signed in to change notification settings - Fork 273
/
Perf.BitArray.cs
170 lines (133 loc) · 5.85 KB
/
Perf.BitArray.cs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
namespace System.Collections.Tests
{
[BenchmarkCategory(Categories.Libraries, Categories.Collections)]
public class Perf_BitArray
{
private const int DefaultShiftCount = 17;
private const bool BooleanValue = true;
// 4 - Small size to test non-vectorised paths
// DefaultCollectionSize - Big enough size to go through the vectorised paths
[Params(4, Utils.DefaultCollectionSize)]
public int Size { get; set; }
private BitArray _original;
private BitArray _original2;
private byte[] _bytes;
private bool[] _bools;
private int[] _ints;
[Benchmark]
public BitArray BitArrayLengthCtor() => new BitArray(Size);
[Benchmark]
public BitArray BitArrayLengthValueCtor() => new BitArray(Size, BooleanValue);
[GlobalSetup(Target = nameof(BitArrayBitArrayCtor))]
public void Setup_BitArrayBitArrayCtor() => _original = new BitArray(Size, BooleanValue);
[Benchmark]
[MemoryRandomization]
public BitArray BitArrayBitArrayCtor() => new BitArray(_original);
[GlobalSetup(Target = nameof(BitArrayBoolArrayCtor))]
public void Setup_BitArrayBoolArrayCtor() => _bools = ValuesGenerator.Array<bool>(Size);
[Benchmark]
public BitArray BitArrayBoolArrayCtor() => new BitArray(_bools);
[GlobalSetup(Targets = new [] { nameof(BitArrayByteArrayCtor), nameof(BitArraySetLengthGrow), nameof(BitArraySetLengthShrink) })]
public void Setup_BitArrayByteArrayCtor() => _bytes = ValuesGenerator.Array<byte>(Size);
[Benchmark]
public BitArray BitArrayByteArrayCtor() => new BitArray(_bytes);
[GlobalSetup(Target = nameof(BitArrayIntArrayCtor))]
public void Setup_BitArrayIntArrayCtor() => _ints = ValuesGenerator.Array<int>(Size);
[Benchmark]
[MemoryRandomization]
public BitArray BitArrayIntArrayCtor() => new BitArray(_ints);
[GlobalSetup(Targets = new [] { nameof(BitArraySetAll), nameof(BitArrayNot), nameof(BitArrayGet) })]
public void Setup_BitArraySetAll() => _original = new BitArray(ValuesGenerator.Array<byte>(Size));
[Benchmark]
[MemoryRandomization]
public void BitArraySetAll() => _original.SetAll(BooleanValue);
[Benchmark]
public BitArray BitArrayNot() => _original.Not();
[Benchmark]
public bool BitArrayGet()
{
bool local = false;
BitArray original = _original;
for (int j = 0; j < original.Length; j++)
local ^= original.Get(j);
return local;
}
#if !NETFRAMEWORK // API added in .NET Core 2.0
[GlobalSetup(Targets = new [] { nameof(BitArrayRightShift), nameof(BitArrayLeftShift) })]
public void Setup_BitArrayShift() => _original = new BitArray(ValuesGenerator.Array<byte>(Size));
[Benchmark]
public void BitArrayRightShift() => _original.RightShift(DefaultShiftCount);
[Benchmark]
public void BitArrayLeftShift() => _original.LeftShift(DefaultShiftCount);
#endif
[GlobalSetup(Targets = new [] { nameof(BitArrayAnd), nameof(BitArrayOr), nameof(BitArrayXor) })]
public void Setup_BitArrayAnd()
{
_original = new BitArray(ValuesGenerator.Array<byte>(Size));
_original2 = new BitArray(ValuesGenerator.Array<byte>(Size));
}
[Benchmark]
public BitArray BitArrayAnd() => _original.And(_original2);
[Benchmark]
public BitArray BitArrayOr() => _original.Or(_original2);
[Benchmark]
public BitArray BitArrayXor() => _original.Xor(_original2);
[GlobalSetup(Target = nameof(BitArraySet))]
public void Setup_BitArraySet() => _original = new BitArray(ValuesGenerator.Array<bool>(Size));
[Benchmark]
public void BitArraySet()
{
BitArray original = _original;
for (int j = 0; j < original.Length; j++)
original.Set(j, BooleanValue);
}
[Benchmark]
[MemoryRandomization]
public BitArray BitArraySetLengthGrow()
{
var original = new BitArray(_bytes);
original.Length = original.Length * 2;
return original;
}
[Benchmark]
public BitArray BitArraySetLengthShrink()
{
var original = new BitArray(_bytes);
original.Length = original.Length / 2;
return original;
}
[GlobalSetup(Target = nameof(BitArrayCopyToIntArray))]
public void Setup_BitArrayCopyToIntArray()
{
_bytes = ValuesGenerator.Array<byte>(Size);
_original = new BitArray(_bytes);
_ints = new int[Size / 4];
}
[Benchmark]
[MemoryRandomization]
public void BitArrayCopyToIntArray() => _original.CopyTo(_ints, 0);
[GlobalSetup(Target = nameof(BitArrayCopyToByteArray))]
public void Setup_BitArrayCopyToByteArray()
{
_bytes = ValuesGenerator.Array<byte>(Size);
_original = new BitArray(_bytes);
}
[Benchmark]
public void BitArrayCopyToByteArray() => _original.CopyTo(_bytes, 0);
[GlobalSetup(Target = nameof(BitArrayCopyToBoolArray))]
public void Setup_BitArrayCopyToBoolArray()
{
_bytes = ValuesGenerator.Array<byte>(Size);
_original = new BitArray(_bytes);
_bools = new bool[Size * 32];
}
[Benchmark]
public void BitArrayCopyToBoolArray() => _original.CopyTo(_bools, 0);
}
}