14
14
public class Benchmarks
15
15
{
16
16
private readonly List < int > _numbers = Enumerable . Range ( 0 , 10000 ) . Select ( s => Random . Shared . Next ( ) ) . ToList ( ) ;
17
+ private readonly HashSet < int > _hashSet = Enumerable . Range ( 0 , 10000 ) . ToHashSet ( ) ;
18
+ private readonly Dictionary < int , int > _dictionary = Enumerable . Range ( 0 , 10000 ) . ToDictionary ( k => k , v => v ) ;
19
+
17
20
private const string Text1 = "Hello woRld! ThIs is a test.It CoUlD be a lot longer, but it'S not. Or is it?" ;
18
21
19
22
private const string Text2 = "Hello world! This is a test.it could be a lot longer, but it's not. Or is it?" ;
@@ -65,10 +68,10 @@ public string InvokeMethodViaReflection()
65
68
public string [ ] EnumGetNames ( ) => Enum . GetNames < DayOfWeek > ( ) ;
66
69
67
70
[ Benchmark ]
68
- public List < int > ListAdd1000 ( )
71
+ public List < int > ListAdd10000 ( )
69
72
{
70
73
var list = new List < int > ( ) ;
71
- for ( var i = 0 ; i < 1000 ; i ++ )
74
+ for ( var i = 0 ; i < 10_000 ; i ++ )
72
75
{
73
76
list . Add ( i ) ;
74
77
}
@@ -77,12 +80,38 @@ public List<int> ListAdd1000()
77
80
}
78
81
79
82
[ Benchmark ]
80
- public int ListLookup ( )
83
+ public int HashSetLookup ( )
84
+ {
85
+ var entriesFound = 0 ;
86
+ for ( var i = 0 ; i < 10000 ; i ++ )
87
+ {
88
+ if ( _hashSet . Contains ( i ) )
89
+ entriesFound ++ ;
90
+ }
91
+
92
+ return entriesFound ;
93
+ }
94
+
95
+ [ Benchmark ]
96
+ public int DictionaryKeyLookup ( )
97
+ {
98
+ var entriesFound = 0 ;
99
+ for ( var i = 0 ; i < 10000 ; i ++ )
100
+ {
101
+ if ( _dictionary . ContainsKey ( i ) )
102
+ entriesFound ++ ;
103
+ }
104
+
105
+ return entriesFound ;
106
+ }
107
+
108
+ [ Benchmark ]
109
+ public int DictionaryValueLookup ( )
81
110
{
82
111
var entriesFound = 0 ;
83
- for ( var i = 0 ; i < 100 ; i ++ )
112
+ for ( var i = 0 ; i < 10000 ; i ++ )
84
113
{
85
- if ( _numbers . Contains ( i ) )
114
+ if ( _dictionary . ContainsValue ( i ) )
86
115
entriesFound ++ ;
87
116
}
88
117
0 commit comments