-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathContainsTrueComparer.cs
126 lines (112 loc) · 4.36 KB
/
ContainsTrueComparer.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
// 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 System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
namespace System.Collections
{
[BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.GenericCollections)]
[GenericTypeArguments(typeof(int))] // value type
[GenericTypeArguments(typeof(string))] // reference type
public class ContainsTrueComparer<T> where T : IEquatable<T>
{
private sealed class WrapDefaultComparer : IEqualityComparer<T>, IComparer<T>
{
public int Compare(T x, T y) => Comparer<T>.Default.Compare(x, y);
public bool Equals(T x, T y) => EqualityComparer<T>.Default.Equals(x, y);
public int GetHashCode(T obj) => EqualityComparer<T>.Default.GetHashCode(obj);
}
private T[] _found;
private HashSet<T> _hashSet;
private SortedSet<T> _sortedSet;
private ImmutableHashSet<T> _immutableHashSet;
private ImmutableSortedSet<T> _immutableSortedSet;
private FrozenSet<T> _frozenSet;
[Params(Utils.DefaultCollectionSize)]
public int Size;
[GlobalSetup(Target = nameof(HashSet))]
public void SetupHashSet()
{
_found = ValuesGenerator.ArrayOfUniqueValues<T>(Size);
_hashSet = new HashSet<T>(_found, new WrapDefaultComparer());
}
[Benchmark]
public bool HashSet()
{
bool result = default;
HashSet<T> collection = _hashSet;
T[] found = _found;
for (int i = 0; i < found.Length; i++)
result ^= collection.Contains(found[i]);
return result;
}
[GlobalSetup(Target = nameof(SortedSet))]
public void SetupSortedSet()
{
_found = ValuesGenerator.ArrayOfUniqueValues<T>(Size);
_sortedSet = new SortedSet<T>(_found, new WrapDefaultComparer());
}
[Benchmark]
public bool SortedSet()
{
bool result = default;
SortedSet<T> collection = _sortedSet;
T[] found = _found;
for (int i = 0; i < found.Length; i++)
result ^= collection.Contains(found[i]);
return result;
}
[GlobalSetup(Target = nameof(ImmutableHashSet))]
public void SetupImmutableHashSet()
{
_found = ValuesGenerator.ArrayOfUniqueValues<T>(Size);
_immutableHashSet = Immutable.ImmutableHashSet.CreateRange(_found).WithComparer(new WrapDefaultComparer());
}
[Benchmark]
public bool ImmutableHashSet()
{
bool result = default;
ImmutableHashSet<T> collection = _immutableHashSet;
T[] found = _found;
for (int i = 0; i < found.Length; i++)
result ^= collection.Contains(found[i]);
return result;
}
[GlobalSetup(Target = nameof(ImmutableSortedSet))]
public void SetupImmutableSortedSet()
{
_found = ValuesGenerator.ArrayOfUniqueValues<T>(Size);
_immutableSortedSet = Immutable.ImmutableSortedSet.CreateRange(_found).WithComparer(new WrapDefaultComparer());
}
[Benchmark]
public bool ImmutableSortedSet()
{
bool result = default;
ImmutableSortedSet<T> collection = _immutableSortedSet;
T[] found = _found;
for (int i = 0; i < found.Length; i++)
result ^= collection.Contains(found[i]);
return result;
}
[GlobalSetup(Target = nameof(FrozenSet))]
public void SetupFrozenSet()
{
_found = ValuesGenerator.ArrayOfUniqueValues<T>(Size);
_frozenSet = _found.ToFrozenSet(new WrapDefaultComparer());
}
[Benchmark]
public bool FrozenSet()
{
bool result = default;
FrozenSet<T> collection = _frozenSet;
T[] found = _found;
for (int i = 0; i < found.Length; i++)
result ^= collection.Contains(found[i]);
return result;
}
}
}