|
| 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 | +} |
0 commit comments