Skip to content

Commit 5548e9e

Browse files
committed
Benchmarks that search for an item in a collection.
1 parent 044711a commit 5548e9e

File tree

2 files changed

+163
-1
lines changed

2 files changed

+163
-1
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System;
2+
using System.Linq;
3+
using System.Collections.Generic;
4+
using System.Collections.Immutable;
5+
using System.Collections.ObjectModel;
6+
using System.Runtime.InteropServices;
7+
using BenchmarkDotNet.Attributes;
8+
9+
namespace Thinktecture.Benchmarks;
10+
11+
[MemoryDiagnoser]
12+
public class ItemSearch
13+
{
14+
public class SmartEnum
15+
{
16+
public string Key { get; }
17+
18+
public SmartEnum(string key)
19+
{
20+
Key = key;
21+
}
22+
}
23+
24+
private readonly Dictionary<string, SmartEnum> _dictionary;
25+
private readonly ReadOnlyDictionary<string, SmartEnum> _readOnlyDictionary;
26+
private readonly ImmutableDictionary<string, SmartEnum> _immutableDictionary;
27+
private readonly ImmutableSortedDictionary<string, SmartEnum> _immutableSortedDictionary;
28+
private readonly List<SmartEnum> _list;
29+
private readonly ReadOnlyCollection<SmartEnum> _readOnlyCollection;
30+
private readonly SmartEnum[] _array;
31+
private readonly string[] _keysArray;
32+
33+
[Params("aaaaAaaaaa", "iiiiIiiiii", "tttttTtttt")]
34+
public string SearchTerm { get; set; } = String.Empty;
35+
36+
public ItemSearch()
37+
{
38+
var dictionary = new Dictionary<string, SmartEnum>(10, StringComparer.OrdinalIgnoreCase);
39+
_immutableDictionary = ImmutableDictionary<string, SmartEnum>.Empty.WithComparers(StringComparer.OrdinalIgnoreCase);
40+
_immutableSortedDictionary = ImmutableSortedDictionary<string, SmartEnum>.Empty.WithComparers(StringComparer.OrdinalIgnoreCase);
41+
var list = new List<SmartEnum>(10);
42+
43+
for (var i = 0; i < 20; i++)
44+
{
45+
var key = new string((char)('a' + i), 10);
46+
var item = new SmartEnum(key);
47+
dictionary.Add(item.Key, item);
48+
_immutableDictionary = _immutableDictionary.Add(item.Key, item);
49+
_immutableSortedDictionary = _immutableSortedDictionary.Add(item.Key, item);
50+
list.Add(item);
51+
}
52+
53+
_dictionary = dictionary;
54+
_readOnlyDictionary = new ReadOnlyDictionary<string, SmartEnum>(dictionary);
55+
_list = list;
56+
_readOnlyCollection = list.AsReadOnly();
57+
_array = list.OrderBy(i => i.Key).ToArray();
58+
_keysArray = _array.Select(i => i.Key).ToArray();
59+
}
60+
61+
[Benchmark]
62+
public SmartEnum? Dictionary()
63+
{
64+
_dictionary.TryGetValue(SearchTerm, out var item);
65+
66+
return item;
67+
}
68+
69+
[Benchmark]
70+
public SmartEnum? ReadOnlyDictionary()
71+
{
72+
_readOnlyDictionary.TryGetValue(SearchTerm, out var item);
73+
74+
return item;
75+
}
76+
77+
[Benchmark]
78+
public SmartEnum? ImmutableDictionary()
79+
{
80+
_immutableDictionary.TryGetValue(SearchTerm, out var item);
81+
82+
return item;
83+
}
84+
85+
[Benchmark]
86+
public SmartEnum? ImmutableSortedDictionary()
87+
{
88+
_immutableSortedDictionary.TryGetValue(SearchTerm, out var item);
89+
90+
return item;
91+
}
92+
93+
// [Benchmark]
94+
// public SmartEnum? List()
95+
// {
96+
// for (var i = 0; i < _list.Count; i++)
97+
// {
98+
// var item = _list[i];
99+
//
100+
// if (SearchTerm.Equals(item.Key, StringComparison.OrdinalIgnoreCase))
101+
// return item;
102+
// }
103+
//
104+
// return null;
105+
// }
106+
//
107+
// [Benchmark]
108+
// public SmartEnum? ListAsSpan()
109+
// {
110+
// var span = CollectionsMarshal.AsSpan(_list);
111+
//
112+
// for (var i = 0; i < span.Length; i++)
113+
// {
114+
// var item = span[i];
115+
//
116+
// if (SearchTerm.Equals(item.Key, StringComparison.OrdinalIgnoreCase))
117+
// return item;
118+
// }
119+
//
120+
// return null;
121+
// }
122+
123+
// [Benchmark]
124+
// public SmartEnum? ReadOnlyList()
125+
// {
126+
// for (var i = 0; i < _readOnlyCollection.Count; i++)
127+
// {
128+
// var item = _readOnlyCollection[i];
129+
//
130+
// if (SearchTerm.Equals(item.Key, StringComparison.OrdinalIgnoreCase))
131+
// return item;
132+
// }
133+
//
134+
// return null;
135+
// }
136+
137+
[Benchmark]
138+
public SmartEnum? ArrayIteration()
139+
{
140+
for (var i = 0; i < _array.Length; i++)
141+
{
142+
var item = _array[i];
143+
144+
if (SearchTerm.Equals(item.Key, StringComparison.OrdinalIgnoreCase))
145+
return item;
146+
}
147+
148+
return null;
149+
}
150+
151+
// [Benchmark]
152+
// public SmartEnum? ArrayBinary()
153+
// {
154+
// var index = Array.BinarySearch(_keysArray, SearchTerm, StringComparer.OrdinalIgnoreCase);
155+
//
156+
// if (index >= 0)
157+
// return _array[index];
158+
//
159+
// return null;
160+
// }
161+
}

samples/Thinktecture.Runtime.Extensions.Benchmarking/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
// BenchmarkRunner.Run<LoadingSmartEnums>();
55
// BenchmarkRunner.Run<LoadingValueObjects>();
66
// BenchmarkRunner.Run<SingleItemCollectionBenchmarks>();
7-
BenchmarkRunner.Run<SingleItemSetBenchmarks>();
7+
// BenchmarkRunner.Run<SingleItemSetBenchmarks>();
8+
BenchmarkRunner.Run<ItemSearch>();

0 commit comments

Comments
 (0)