-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmapSequentialContainsTrue.cs
More file actions
71 lines (59 loc) · 1.63 KB
/
Copy pathBitmapSequentialContainsTrue.cs
File metadata and controls
71 lines (59 loc) · 1.63 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
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
namespace Benchmarkator.Bitmap;
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
public class BitmapSequentialContainsTrue
{
[Params(32, 1024)]
public int Length;
private System.Collections.BitArray _bitArray = null!;
private Dictionary<int, bool> _map = null!;
private HashSet<int> _set = null!;
[GlobalSetup]
public void Setup()
{
// only `true` values are stored, missing value means bit is not set
_bitArray = new System.Collections.BitArray(Length, true);
_map = new Dictionary<int, bool>(
Enumerable
.Range(0, Length)
.Select(v => KeyValuePair.Create(v, true)));
_set = new HashSet<int>(
Enumerable.Range(0, Length));
}
[Benchmark]
public bool BitArrayContains()
{
var contains = false;
for (var i = 0; i < Length; i++)
{
// or: `_bitArray[i]`
contains |= _bitArray.Get(i);
}
return contains;
}
[Benchmark]
public bool DictionaryContains()
{
var contains = false;
for (var i = 0; i < Length; i++)
{
// NB! value is `true`, see setup
// (alternatively: `_map.ContainsKey(i)`)
contains |= _map[i];
}
return contains;
}
[Benchmark]
public bool SetContains()
{
var contains = false;
for (var i = 0; i < Length; i++)
{
contains |= _set.Contains(i);
}
return contains;
}
}