TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

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.

ContainsKey

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.

TryGetValue
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf