C# Dictionary Optimization, Test With ContainsKey

Measure the performance of testing for the existence of keys in a Dictionary instance.

Dictionary test. Suppose you have a Dictionary collection. You are setting many different keys all to the same value. Would it be faster to test for the existence of a key before setting it again? We explore this interesting issue.DictionaryOptimization

This benchmark simply sets the value of the Dictionary entry at the key "test" to true. It repeatedly sets the same key to true. It compares storing a value in the Dictionary versus testing before storing the value.
Note: The comparison here would be invalid if you were changing the value. It would not apply.
Info: The program found that calling ContainsKey on a key that exists is faster here than storing a value in the Dictionary.
So: If a key is very likely to already exist in the Dictionary, it is best to leave it alone and not reset it.
C# program that benchmarks Dictionary using System; using System.Collections.Generic; using System.Diagnostics; class Program { const int _max = 100000000; static void Main() { var dict = new Dictionary<string, bool>(StringComparer.Ordinal); SetTrue1(dict, "test"); SetTrue2(dict, "test"); var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { SetTrue1(dict, "test"); } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { SetTrue2(dict, "test"); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.Read(); } static void SetTrue1(Dictionary<string, bool> dict, string key) { dict[key] = true; } static void SetTrue2(Dictionary<string, bool> dict, string key) { if (!dict.ContainsKey(key)) { dict[key] = true; } } } Output 30.81 ns SetTrue1 29.05 ns SetTrue2

Discussion. Storing data in a Dictionary is more expensive overall than testing for the existence of that data. Thus, if a key is likely to already exist, it is probably better to test for it with ContainsKey instead of writing to it again.
Also: You could use TryGetValue and test to see if the value you want to add is also the same as the one that exists.

Summary. The hashtable is an important data structure implemented by the Dictionary type. With the C# Dictionary, careful testing of the existence of keys can lead to performance gains. The benefits are modest.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.