-
Notifications
You must be signed in to change notification settings - Fork 273
/
TryGetValueFalse.cs
145 lines (130 loc) · 5.7 KB
/
TryGetValueFalse.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
// 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.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
namespace System.Collections
{
[BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.GenericCollections)]
[GenericTypeArguments(typeof(int), typeof(int))] // primitive value type
[GenericTypeArguments(typeof(BigStruct), typeof(BigStruct))] // big value type
[GenericTypeArguments(typeof(SmallClass), typeof(SmallClass))] // reference type
[GenericTypeArguments(typeof(string), typeof(string))] // reference type
public class TryGetValueFalse<TKey, TValue>
{
private TKey[] _notFound;
private Dictionary<TKey, TValue> _source;
private Dictionary<TKey, TValue> _dictionary;
private SortedList<TKey, TValue> _sortedList;
private SortedDictionary<TKey, TValue> _sortedDictionary;
private ConcurrentDictionary<TKey, TValue> _concurrentDictionary;
private ImmutableDictionary<TKey, TValue> _immutableDictionary;
private ImmutableSortedDictionary<TKey, TValue> _immutableSortedDictionary;
private FrozenDictionary<TKey, TValue> _frozenDictionary;
[Params(Utils.DefaultCollectionSize)]
public int Size;
[GlobalSetup]
public void Setup()
{
var values = ValuesGenerator.ArrayOfUniqueValues<TKey>(Size * 2);
_notFound = values.Take(Size).ToArray();
_source = values.Skip(Size).Take(Size).ToDictionary(item => item, item => (TValue)(object)item);
_dictionary = new Dictionary<TKey, TValue>(_source);
_sortedList = new SortedList<TKey, TValue>(_source);
_sortedDictionary = new SortedDictionary<TKey, TValue>(_source);
_concurrentDictionary = new ConcurrentDictionary<TKey, TValue>(_source);
_immutableDictionary = Immutable.ImmutableDictionary.CreateRange<TKey, TValue>(_source);
_immutableSortedDictionary = Immutable.ImmutableSortedDictionary.CreateRange<TKey, TValue>(_source);
_frozenDictionary = _source.ToFrozenDictionary();
}
[Benchmark]
public bool Dictionary()
{
bool result = default;
Dictionary<TKey, TValue> collection = _dictionary;
TKey[] notFound = _notFound;
for (int i = 0; i < notFound.Length; i++)
result ^= collection.TryGetValue(notFound[i], out _);
return result;
}
[Benchmark]
[BenchmarkCategory(Categories.Runtime, Categories.Virtual)]
public bool IDictionary() => TryGetValue(_dictionary);
[MethodImpl(MethodImplOptions.NoInlining)]
private bool TryGetValue(IDictionary<TKey, TValue> collection)
{
bool result = default;
TKey[] notFound = _notFound;
for (int i = 0; i < notFound.Length; i++)
result ^= collection.TryGetValue(notFound[i], out _);
return result;
}
[Benchmark]
public bool SortedList()
{
bool result = default;
SortedList<TKey, TValue> collection = _sortedList;
TKey[] notFound = _notFound;
for (int i = 0; i < notFound.Length; i++)
result ^= collection.TryGetValue(notFound[i], out _);
return result;
}
[Benchmark]
public bool SortedDictionary()
{
bool result = default;
SortedDictionary<TKey, TValue> collection = _sortedDictionary;
TKey[] notFound = _notFound;
for (int i = 0; i < notFound.Length; i++)
result ^= collection.TryGetValue(notFound[i], out _);
return result;
}
[Benchmark]
public bool ConcurrentDictionary()
{
bool result = default;
ConcurrentDictionary<TKey, TValue> collection = _concurrentDictionary;
TKey[] notFound = _notFound;
for (int i = 0; i < notFound.Length; i++)
result ^= collection.TryGetValue(notFound[i], out _);
return result;
}
[Benchmark]
public bool ImmutableDictionary()
{
bool result = default;
ImmutableDictionary<TKey, TValue> collection = _immutableDictionary;
TKey[] notFound = _notFound;
for (int i = 0; i < notFound.Length; i++)
result ^= collection.TryGetValue(notFound[i], out _);
return result;
}
[Benchmark]
public bool ImmutableSortedDictionary()
{
bool result = default;
ImmutableSortedDictionary<TKey, TValue> collection = _immutableSortedDictionary;
TKey[] notFound = _notFound;
for (int i = 0; i < notFound.Length; i++)
result ^= collection.TryGetValue(notFound[i], out _);
return result;
}
[Benchmark(Description = "FrozenDictionary")]
public bool FrozenDictionaryOptimized() // we kept the old name on purpose to avoid loosing historical data
{
bool result = default;
FrozenDictionary<TKey, TValue> collection = _frozenDictionary;
TKey[] notFound = _notFound;
for (int i = 0; i < notFound.Length; i++)
result ^= collection.TryGetValue(notFound[i], out _);
return result;
}
}
}