TheDeveloperBlog.com

Home | Contact Us

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

C# Dictionary Clear

This C# performance article tests the Clear method on the Dictionary type.

Clear erases all Dictionary elements.

In most cases, testing a Dictionary before using it is the best strategy. We explore this theory. We see whether you should test for an empty Dictionary before clearing one.

Example. Collections such as the Dictionary are complex. Some actions on them may be slower than you think. The Count and Clear members both seem simple. But the Count property is much faster than Clear.

So: For this reason, if you may have an empty Dictionary, it is best to call Count before calling Clear.

And: You can avoid clearing in the case where Count returns 0 and there are zero elements.

Count Dictionary

C# program that tests Clear

using System;
using System.Collections.Generic;
using System.Diagnostics;

class Program
{
    const int _max = 100000000;
    static void Main()
    {
	var dict = new Dictionary<string, string>();
	var s1 = Stopwatch.StartNew();
	for (int i = 0; i < _max; i++)
	{
	    dict.Clear();
	}
	s1.Stop();
	var s2 = Stopwatch.StartNew();
	for (int i = 0; i < _max; i++)
	{
	    if (dict.Count > 0)
	    {
		dict.Clear();
	    }
	}
	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();
    }
}

Output

3.17 ns
0.64 ns

Discussion. With the Dictionary, it is faster to check for zero elements before clearing it if this case is common. Overall it seems faster to check for possible cases where no action should be taken on the Dictionary.

And: This is also true with the indexer to add a key: it is faster to call TryGetValue or ContainsKey rather than call the indexer.

IndexerTryGetValueContainsKey

Summary. Using the Dictionary efficiently is an important optimization strategy in many C# programs. By storing data in memory, you can avoid a lot of computations, disk accesses, or even network loads.

And: The Clear method too can be used in a more efficient way. This avoids unneeded computations.


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