Skip to content

Commit 26fed4c

Browse files
committed
Add FrozenCollection Benchmark
1 parent f3a237d commit 26fed4c

File tree

4 files changed

+231
-6
lines changed

4 files changed

+231
-6
lines changed

src/Tests/CollectionTest/CollectionTest.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
12+
</ItemGroup>
13+
1014
</Project>

src/Tests/CollectionTest/ConditionalWeakTableTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static void Test()
7979

8080
#endregion
8181

82-
private class ManagedClass
82+
private sealed class ManagedClass
8383
{
8484

8585
#region Properties
@@ -89,7 +89,7 @@ private class ManagedClass
8989
#endregion
9090
}
9191

92-
private class ClassData
92+
private sealed class ClassData
9393
{
9494
public ClassData()
9595
{
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Configs;
3+
using System.Collections.Frozen;
4+
using System.Collections.Immutable;
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
namespace CollectionTest;
8+
9+
//[Orderer(SummaryOrderPolicy.FastestToSlowest)]
10+
//[RankColumn(NumeralSystem.Arabic)]
11+
//[MarkdownExporter]
12+
[MemoryDiagnoser]
13+
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
14+
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "<Pending>")]
15+
public class FrozenCollectionBenchmark
16+
{
17+
18+
#region Constants & Statics
19+
20+
private const int itemsCount = 100_000;
21+
private const int keyToFind = 500;
22+
23+
#endregion
24+
25+
private readonly Dictionary<int, int> _dictionary = Enumerable.Range(0, itemsCount).ToDictionary(key => key);
26+
27+
private readonly FrozenDictionary<int, int> _frozenDictionary = Enumerable.Range(0, itemsCount)
28+
.ToFrozenDictionary(key => key);
29+
private readonly FrozenSet<int> _frozenSet = Enumerable.Range(0, itemsCount).ToFrozenSet();
30+
31+
private readonly HashSet<int> _hashSet = Enumerable.Range(0, itemsCount).ToHashSet();
32+
private readonly ImmutableDictionary<int, int> _immutableDictionary = Enumerable.Range(0, itemsCount)
33+
.ToImmutableDictionary(key => key);
34+
private readonly ImmutableHashSet<int> _immutableHashSet = Enumerable.Range(0, itemsCount).ToImmutableHashSet();
35+
private readonly List<int> _list = Enumerable.Range(0, itemsCount).ToList();
36+
37+
#region GetValue
38+
39+
[Benchmark]
40+
[BenchmarkCategory("GetValue")]
41+
public void TryGetValueDictionary()
42+
{
43+
_ = _dictionary.TryGetValue(keyToFind, out _);
44+
}
45+
46+
[Benchmark]
47+
[BenchmarkCategory("GetValue")]
48+
public void TryGetValueFrozenDictionary()
49+
{
50+
_ = _frozenDictionary.TryGetValue(keyToFind, out _);
51+
}
52+
53+
[Benchmark]
54+
[BenchmarkCategory("GetValue")]
55+
public void TryGetValueFrozenSet()
56+
{
57+
_ = _frozenSet.TryGetValue(keyToFind, out _);
58+
}
59+
60+
[Benchmark]
61+
[BenchmarkCategory("GetValue")]
62+
public void TryGetValueHashSet()
63+
{
64+
_ = _hashSet.TryGetValue(keyToFind, out _);
65+
}
66+
67+
[Benchmark]
68+
[BenchmarkCategory("GetValue")]
69+
public void TryGetValueImmutableDictionary()
70+
{
71+
_ = _immutableDictionary.TryGetValue(keyToFind, out _);
72+
}
73+
74+
[Benchmark]
75+
[BenchmarkCategory("GetValue")]
76+
public void TryGetValueImmutableHashSet()
77+
{
78+
_ = _immutableHashSet.TryGetValue(keyToFind, out _);
79+
}
80+
81+
[Benchmark(Baseline = true)]
82+
[BenchmarkCategory("GetValue")]
83+
public void TryGetValueList()
84+
{
85+
_ = _list.FirstOrDefault(o => o == keyToFind);
86+
}
87+
88+
#endregion
89+
90+
#region Create
91+
92+
[Benchmark]
93+
[BenchmarkCategory("Create")]
94+
public void CreateDictionary()
95+
{
96+
var dictionary = Enumerable.Range(0, itemsCount).ToDictionary(key => key);
97+
}
98+
99+
[Benchmark]
100+
[BenchmarkCategory("Create")]
101+
public void CreateFrozenDictionary()
102+
{
103+
var frozenDictionary = Enumerable.Range(0, itemsCount).ToFrozenDictionary(key => key);
104+
}
105+
106+
[Benchmark]
107+
[BenchmarkCategory("Create")]
108+
public void CreateFrozenSet()
109+
{
110+
_ = Enumerable.Range(0, itemsCount).ToFrozenSet();
111+
}
112+
113+
[Benchmark]
114+
[BenchmarkCategory("Create")]
115+
public void CreateHashSet()
116+
{
117+
_ = Enumerable.Range(0, itemsCount).ToHashSet();
118+
}
119+
120+
[Benchmark]
121+
[BenchmarkCategory("Create")]
122+
public void CreateImmutableDictionary()
123+
{
124+
var dictionary = Enumerable.Range(0, itemsCount).ToImmutableDictionary(key => key);
125+
}
126+
127+
[Benchmark]
128+
[BenchmarkCategory("Create")]
129+
public void CreateImmutableHashSet()
130+
{
131+
_ = Enumerable.Range(0, itemsCount).ToImmutableHashSet();
132+
}
133+
134+
[Benchmark(Baseline = true)]
135+
[BenchmarkCategory("Create")]
136+
public void CreateList()
137+
{
138+
_ = Enumerable.Range(0, itemsCount).ToList();
139+
}
140+
141+
#endregion
142+
143+
#region Foreach
144+
145+
[Benchmark]
146+
[BenchmarkCategory("Foreach")]
147+
public void ForeachDictionary()
148+
{
149+
foreach (var item in _dictionary)
150+
{
151+
_ = item.Key;
152+
}
153+
}
154+
155+
[Benchmark]
156+
[BenchmarkCategory("Foreach")]
157+
public void ForeachFrozenDictionary()
158+
{
159+
foreach (var item in _frozenDictionary)
160+
{
161+
_ = item.Key;
162+
}
163+
}
164+
165+
[Benchmark]
166+
[BenchmarkCategory("Foreach")]
167+
public void ForeachFrozenSet()
168+
{
169+
foreach (var item in _frozenSet)
170+
{
171+
_ = item;
172+
}
173+
}
174+
175+
[Benchmark]
176+
[BenchmarkCategory("Foreach")]
177+
public void ForeachHashSet()
178+
{
179+
foreach (var item in _hashSet)
180+
{
181+
_ = item;
182+
}
183+
}
184+
185+
[Benchmark]
186+
[BenchmarkCategory("Foreach")]
187+
public void ForeachImmutableDictionary()
188+
{
189+
foreach (var item in _immutableDictionary)
190+
{
191+
_ = item.Key;
192+
}
193+
}
194+
195+
[Benchmark]
196+
[BenchmarkCategory("Foreach")]
197+
public void ForeachImmutableHashSet()
198+
{
199+
foreach (var item in _immutableHashSet)
200+
{
201+
_ = item;
202+
}
203+
}
204+
205+
[Benchmark(Baseline = true)]
206+
[BenchmarkCategory("Foreach")]
207+
public void ForeachList()
208+
{
209+
foreach (var item in _list)
210+
{
211+
_ = item;
212+
}
213+
}
214+
215+
#endregion
216+
217+
}

src/Tests/CollectionTest/Program.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using BenchmarkDotNet.Running;
2+
13
namespace CollectionTest;
24

3-
internal class Program
5+
internal sealed class Program
46
{
57

68
#region Constants & Statics
@@ -11,9 +13,11 @@ private static void Main(string[] args)
1113

1214
//SortedListTests.Order_Test();
1315

14-
ConditionalWeakTableTests.Test();
16+
//ConditionalWeakTableTests.Test();
17+
18+
//Console.ReadLine();
1519

16-
Console.ReadLine();
20+
BenchmarkRunner.Run<FrozenCollectionBenchmark>();
1721
}
1822

1923
#endregion

0 commit comments

Comments
 (0)