Benchmark of Collection Searching (Contains method) in terms of Execution Time (Mean)
BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3007/23H2/2023Update/SunValley3)
AMD Ryzen 7 5800H with Radeon Graphics, 1 CPU, 16 logical and 8 physical cores
.NET SDK 8.0.101
[Host] : .NET 8.0.1 (8.0.123.58001), X64 RyuJIT AVX2
ShortRun : .NET 8.0.1 (8.0.123.58001), X64 RyuJIT AVX2
Job=ShortRun IterationCount=3 LaunchCount=1
WarmupCount=3
Comparison: Mean
Method | Big O | Length | Existed | Int32 | String | StructInts | ClassInts | RecordStructInts | RecordClassInts | StructStrings | ClassStrings | RecordStructStrings | RecordClassStrings |
FrozenSet |
O(1) |
1000 |
False |
1.945 ns |
4.308 ns |
20.32 ns |
3.816 ns |
2.595 ns |
5.191 ns |
60.97 ns |
23.75 ns |
22.21 ns |
26.26 ns |
FrozenDictionary |
O(1) |
1000 |
False |
2.029 ns |
4.405 ns |
20.49 ns |
3.970 ns |
2.598 ns |
5.176 ns |
56.80 ns |
23.77 ns |
22.56 ns |
25.92 ns |
Dictionary |
O(1) |
1000 |
False |
3.151 ns |
7.015 ns |
24.19 ns |
5.605 ns |
4.380 ns |
7.726 ns |
57.91 ns |
26.48 ns |
25.28 ns |
28.83 ns |
HashSet |
O(1) |
1000 |
False |
3.557 ns |
6.841 ns |
22.29 ns |
5.896 ns |
4.425 ns |
7.549 ns |
57.13 ns |
25.09 ns |
24.82 ns |
28.05 ns |
ReadOnlyDictionary |
O(1) |
1000 |
False |
3.620 ns |
8.961 ns |
22.84 ns |
7.707 ns |
4.576 ns |
9.805 ns |
57.50 ns |
27.76 ns |
25.06 ns |
31.49 ns |
ImmutableDictionary |
O(1) |
1000 |
False |
6.936 ns |
16.405 ns |
25.33 ns |
10.967 ns |
7.573 ns |
12.270 ns |
62.08 ns |
30.32 ns |
25.81 ns |
33.48 ns |
ImmutableHashSet |
O(1) |
1000 |
False |
7.002 ns |
26.570 ns |
27.69 ns |
24.802 ns |
7.642 ns |
24.816 ns |
59.30 ns |
40.72 ns |
25.23 ns |
42.86 ns |
SortedSet |
O(log(N)) |
1000 |
False |
12.098 ns |
307.629 ns |
12.96 ns |
40.534 ns |
13.978 ns |
42.957 ns |
296.09 ns |
318.67 ns |
308.11 ns |
317.64 ns |
SortedList |
O(log(N)) |
1000 |
False |
14.066 ns |
322.710 ns |
23.86 ns |
46.525 ns |
23.771 ns |
46.912 ns |
368.81 ns |
327.96 ns |
316.30 ns |
336.31 ns |
Array (Sorted + BinarySearch) |
O(log(N)) |
1000 |
False |
15.108 ns |
314.854 ns |
18.93 ns |
36.756 ns |
18.674 ns |
38.017 ns |
294.83 ns |
320.15 ns |
329.15 ns |
339.42 ns |
ImmutableArray (Sorted + BinarySearch) |
O(log(N)) |
1000 |
False |
15.658 ns |
313.269 ns |
19.19 ns |
39.942 ns |
18.848 ns |
40.870 ns |
304.61 ns |
326.21 ns |
302.08 ns |
329.34 ns |
List (Sorted + BinarySearch) |
O(log(N)) |
1000 |
False |
19.406 ns |
312.884 ns |
19.17 ns |
37.883 ns |
19.021 ns |
36.330 ns |
298.54 ns |
318.19 ns |
346.24 ns |
323.40 ns |
SortedDictionary |
O(log(N)) |
1000 |
False |
23.119 ns |
361.296 ns |
24.52 ns |
63.056 ns |
24.974 ns |
62.616 ns |
325.09 ns |
364.69 ns |
329.55 ns |
351.50 ns |
ImmutableSortedDictionary |
O(log(N)) |
1000 |
False |
23.298 ns |
330.652 ns |
37.15 ns |
50.695 ns |
36.904 ns |
53.115 ns |
323.76 ns |
332.48 ns |
319.98 ns |
331.05 ns |
ImmutableSortedSet |
O(log(N)) |
1000 |
False |
23.324 ns |
323.908 ns |
37.11 ns |
52.178 ns |
37.512 ns |
52.308 ns |
309.61 ns |
333.72 ns |
317.71 ns |
328.30 ns |
ImmutableList (Sorted + BinarySearch) |
O(log(N)) |
1000 |
False |
28.372 ns |
356.776 ns |
31.48 ns |
77.122 ns |
31.460 ns |
77.621 ns |
320.08 ns |
366.50 ns |
317.95 ns |
360.58 ns |
List |
O(N) |
1000 |
False |
43.877 ns |
3,747.693 ns |
13,190.33 ns |
1,734.703 ns |
495.131 ns |
2,750.610 ns |
115,935.57 ns |
4,067.15 ns |
2,531.53 ns |
4,809.68 ns |
ReadOnlyCollection |
O(N) |
1000 |
False |
44.524 ns |
2,851.432 ns |
13,302.69 ns |
773.154 ns |
512.845 ns |
1,981.269 ns |
111,861.43 ns |
791.51 ns |
2,520.05 ns |
4,265.90 ns |
ImmutableArray |
O(N) |
1000 |
False |
68.765 ns |
4,042.309 ns |
13,202.27 ns |
1,762.379 ns |
493.708 ns |
2,708.779 ns |
109,484.52 ns |
3,974.60 ns |
2,730.36 ns |
5,396.69 ns |
Array |
O(N) |
1000 |
False |
70.900 ns |
4,171.897 ns |
12,463.03 ns |
1,495.015 ns |
484.841 ns |
2,741.353 ns |
116,172.02 ns |
4,236.23 ns |
2,848.28 ns |
4,688.74 ns |
Method | Big O | Length | Existed | Int32 | String | StructInts | ClassInts | RecordStructInts | RecordClassInts | StructStrings | ClassStrings | RecordStructStrings | RecordClassStrings |
FrozenDictionary |
O(1) |
1000 |
True |
2.105 ns |
4.987 ns |
36.45 ns |
7.667 ns |
3.785 ns |
8.955 ns |
263.21 ns |
27.90 ns |
24.25 ns |
29.58 ns |
FrozenSet |
O(1) |
1000 |
True |
2.348 ns |
4.452 ns |
35.92 ns |
7.293 ns |
3.618 ns |
9.076 ns |
256.57 ns |
27.47 ns |
23.66 ns |
30.00 ns |
HashSet |
O(1) |
1000 |
True |
3.854 ns |
8.720 ns |
39.24 ns |
9.332 ns |
4.911 ns |
11.447 ns |
271.87 ns |
30.39 ns |
27.57 ns |
31.74 ns |
Dictionary |
O(1) |
1000 |
True |
3.885 ns |
8.825 ns |
41.99 ns |
10.268 ns |
4.986 ns |
11.259 ns |
276.13 ns |
29.96 ns |
26.85 ns |
32.38 ns |
ReadOnlyDictionary |
O(1) |
1000 |
True |
4.289 ns |
11.300 ns |
40.91 ns |
11.855 ns |
5.399 ns |
13.506 ns |
266.99 ns |
32.11 ns |
27.11 ns |
34.29 ns |
ImmutableDictionary |
O(1) |
1000 |
True |
6.075 ns |
21.104 ns |
40.29 ns |
17.843 ns |
7.904 ns |
18.264 ns |
289.31 ns |
34.77 ns |
32.56 ns |
37.16 ns |
ImmutableHashSet |
O(1) |
1000 |
True |
6.100 ns |
28.374 ns |
41.37 ns |
28.555 ns |
7.693 ns |
27.320 ns |
280.04 ns |
45.61 ns |
26.00 ns |
46.25 ns |
SortedSet |
O(log(N)) |
1000 |
True |
11.001 ns |
272.019 ns |
17.81 ns |
37.702 ns |
17.640 ns |
39.288 ns |
247.00 ns |
266.93 ns |
254.76 ns |
267.18 ns |
ImmutableArray (Sorted + BinarySearch) |
O(log(N)) |
1000 |
True |
13.082 ns |
267.570 ns |
20.27 ns |
39.211 ns |
20.637 ns |
39.000 ns |
230.65 ns |
246.53 ns |
228.03 ns |
252.85 ns |
Array (Sorted + BinarySearch) |
O(log(N)) |
1000 |
True |
13.183 ns |
268.204 ns |
20.24 ns |
36.618 ns |
20.843 ns |
37.091 ns |
221.92 ns |
247.97 ns |
226.55 ns |
244.25 ns |
SortedList |
O(log(N)) |
1000 |
True |
14.344 ns |
271.979 ns |
25.58 ns |
43.436 ns |
26.691 ns |
43.424 ns |
252.39 ns |
259.50 ns |
240.87 ns |
252.07 ns |
List (Sorted + BinarySearch) |
O(log(N)) |
1000 |
True |
14.669 ns |
266.752 ns |
20.60 ns |
37.077 ns |
20.606 ns |
36.491 ns |
224.35 ns |
252.79 ns |
228.34 ns |
278.04 ns |
SortedDictionary |
O(log(N)) |
1000 |
True |
16.904 ns |
280.641 ns |
24.90 ns |
58.380 ns |
27.407 ns |
58.743 ns |
282.88 ns |
278.50 ns |
276.39 ns |
282.38 ns |
ImmutableSortedDictionary |
O(log(N)) |
1000 |
True |
18.442 ns |
241.350 ns |
29.41 ns |
42.858 ns |
27.080 ns |
42.221 ns |
248.20 ns |
290.57 ns |
243.17 ns |
268.77 ns |
ImmutableSortedSet |
O(log(N)) |
1000 |
True |
18.480 ns |
248.392 ns |
29.56 ns |
41.586 ns |
29.574 ns |
42.081 ns |
241.52 ns |
253.33 ns |
244.96 ns |
259.55 ns |
ImmutableList (Sorted + BinarySearch) |
O(log(N)) |
1000 |
True |
24.008 ns |
264.561 ns |
27.15 ns |
64.249 ns |
27.141 ns |
64.700 ns |
275.92 ns |
290.76 ns |
247.42 ns |
281.55 ns |
List |
O(N) |
1000 |
True |
29.495 ns |
1,836.948 ns |
6,377.23 ns |
841.805 ns |
252.419 ns |
1,315.648 ns |
52,441.47 ns |
1,995.30 ns |
1,336.23 ns |
2,511.55 ns |
Array |
O(N) |
1000 |
True |
30.105 ns |
1,838.527 ns |
6,322.55 ns |
722.915 ns |
255.642 ns |
1,314.054 ns |
52,394.98 ns |
1,930.61 ns |
1,336.30 ns |
2,276.44 ns |
ReadOnlyCollection |
O(N) |
1000 |
True |
30.159 ns |
1,392.430 ns |
6,487.09 ns |
378.222 ns |
253.197 ns |
956.827 ns |
53,566.95 ns |
392.40 ns |
1,562.09 ns |
1,961.72 ns |
ImmutableArray |
O(N) |
1000 |
True |
30.334 ns |
1,869.762 ns |
6,373.33 ns |
850.812 ns |
257.205 ns |
1,309.247 ns |
56,109.08 ns |
1,914.95 ns |
1,450.75 ns |
2,320.26 ns |