Skip to content

Commit 5dc91c4

Browse files
committed
Add SortedDictionary to benchmarking
1 parent 01e4829 commit 5dc91c4

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

Benchmarking/BenchmarkTests.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ public class DictListBenchmarks
1313

1414
private Dictionary<int, int> _masterDict = new();
1515

16+
private SortedDictionary<int, int> _masterSortedDict = new();
17+
1618
private DictionaryList<int> _masterDictList = new();
1719

1820
private List<int> _iterList = [];
1921

2022
private Dictionary<int, int> _iterDict = new();
2123

24+
private SortedDictionary<int, int> _iterSortedDict = new();
25+
2226
private DictionaryList<int> _iterDictList = new();
2327

2428
[GlobalSetup]
@@ -30,13 +34,15 @@ public void PopulateStuff()
3034
_masterList.TrimExcess();
3135
_masterDict.Clear();
3236
_masterDict.TrimExcess();
37+
_masterSortedDict.Clear();
3338
_masterDictList.Clear();
3439
_masterDictList.CompactAndTrimExcess();
3540

3641
for (var i = 0; i < N; i++)
3742
{
3843
_masterList.Add(i);
3944
_masterDict.Add(i, i);
45+
_masterSortedDict.Add(i, i);
4046
_masterDictList.Add(i);
4147
}
4248
}
@@ -46,9 +52,12 @@ public void CloneStuff()
4652
{
4753
_iterList = new List<int>(_masterList);
4854
_iterDict = new Dictionary<int, int>(_masterDict);
55+
_iterSortedDict = new SortedDictionary<int, int>(_masterSortedDict);
4956
_iterDictList = new DictionaryList<int>(_masterDictList);
5057
}
5158

59+
#region AppendMany
60+
5261
[Benchmark]
5362
public void AppendManyToList()
5463
{
@@ -69,6 +78,16 @@ public void AppendManyToDict()
6978
}
7079
}
7180

81+
[Benchmark]
82+
public void AppendManyToSortedDict()
83+
{
84+
var sortedDict = new SortedDictionary<int, int>();
85+
for (var i = 0; i < N; i++)
86+
{
87+
sortedDict[i] = i;
88+
}
89+
}
90+
7291
[Benchmark]
7392
public void AppendManyToDictList()
7493
{
@@ -79,6 +98,10 @@ public void AppendManyToDictList()
7998
}
8099
}
81100

101+
#endregion
102+
103+
#region Iterate
104+
82105
[Benchmark]
83106
public void IterateList()
84107
{
@@ -99,6 +122,16 @@ public void IterateDict()
99122
}
100123
}
101124

125+
[Benchmark]
126+
public void IterateSortedDict()
127+
{
128+
var sum = 0L;
129+
foreach (var kv in _masterSortedDict)
130+
{
131+
sum += kv.Value;
132+
}
133+
}
134+
102135
[Benchmark]
103136
public void IterateDictList()
104137
{
@@ -109,6 +142,10 @@ public void IterateDictList()
109142
}
110143
}
111144

145+
#endregion
146+
147+
#region ReadMany
148+
112149
[Benchmark]
113150
public void ReadManyFromList()
114151
{
@@ -129,6 +166,16 @@ public void ReadManyFromDict()
129166
}
130167
}
131168

169+
[Benchmark]
170+
public void ReadManyFromSortedDict()
171+
{
172+
// access all indexes of multiples of 5
173+
for (var i = 0; i < N; i += 5)
174+
{
175+
_ = _iterSortedDict[i];
176+
}
177+
}
178+
132179
[Benchmark]
133180
public void ReadManyFromDictList()
134181
{
@@ -139,6 +186,10 @@ public void ReadManyFromDictList()
139186
}
140187
}
141188

189+
#endregion
190+
191+
#region RemoveMany
192+
142193
[Benchmark]
143194
public void RemoveManyFromListInPlace()
144195
{
@@ -181,6 +232,33 @@ public void RemoveManyFromDictWithLinq()
181232
_iterDict = _iterDict.Where(kv => kv.Key % 6 == 0).ToDictionary();
182233
}
183234

235+
[Benchmark]
236+
public void RemoveManyFromSortedDictInPlace()
237+
{
238+
// remove everything not divisible by 6
239+
foreach (var kv in _iterSortedDict)
240+
{
241+
if (kv.Key % 6 == 0)
242+
{
243+
continue;
244+
}
245+
_iterDict.Remove(kv.Key);
246+
}
247+
}
248+
249+
[Benchmark]
250+
public void RemoveManyFromSortedDictWithLinq()
251+
{
252+
// remove everything not divisible by 6
253+
var temp = _iterDict.Where(kv => kv.Key % 6 == 0).ToDictionary();
254+
var target = new SortedDictionary<int, int>();
255+
foreach (var kv in temp)
256+
{
257+
target.Add(kv.Key, kv.Value);
258+
}
259+
_iterSortedDict = target;
260+
}
261+
184262
[Benchmark]
185263
public void RemoveManyFromDictListInPlace()
186264
{
@@ -207,4 +285,6 @@ public void RemoveManyFromDictListWithLinq()
207285
}
208286
_iterDictList = target;
209287
}
288+
289+
#endregion
210290
}

0 commit comments

Comments
 (0)