1414public class Benchmarks
1515{
1616 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+
1720 private const string Text1 = "Hello woRld! ThIs is a test.It CoUlD be a lot longer, but it'S not. Or is it?" ;
1821
1922 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()
6568 public string [ ] EnumGetNames ( ) => Enum . GetNames < DayOfWeek > ( ) ;
6669
6770 [ Benchmark ]
68- public List < int > ListAdd1000 ( )
71+ public List < int > ListAdd10000 ( )
6972 {
7073 var list = new List < int > ( ) ;
71- for ( var i = 0 ; i < 1000 ; i ++ )
74+ for ( var i = 0 ; i < 10_000 ; i ++ )
7275 {
7376 list . Add ( i ) ;
7477 }
@@ -77,12 +80,38 @@ public List<int> ListAdd1000()
7780 }
7881
7982 [ 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 ( )
81110 {
82111 var entriesFound = 0 ;
83- for ( var i = 0 ; i < 100 ; i ++ )
112+ for ( var i = 0 ; i < 10000 ; i ++ )
84113 {
85- if ( _numbers . Contains ( i ) )
114+ if ( _dictionary . ContainsValue ( i ) )
86115 entriesFound ++ ;
87116 }
88117
0 commit comments