C # Dictionary Collection Hashtable, Dictionary, concurrentdictionary three differences

Source: Internet
Author: User

Learning Reference Blog: http://www.cnblogs.com/yinrq/p/5584885.html

Use the Stopwatch class to test time-consuming code:

Using system;using system.collections;using system.collections.concurrent;using system.collections.generic;using        System.diagnostics;namespace webapp{class Program {static Hashtable _hashtable;        Static dictionary<string, string> _dictionary;        Static concurrentdictionary<string, string> _condictionary;            static void Main (string[] args) {Compare (5000000);            Console.ReadLine ();        Console.read ();            } public static void Compare (int datacount) {_hashtable = new Hashtable ();            _dictionary = new dictionary<string, string> ();            _condictionary = new concurrentdictionary<string, string> ();            Stopwatch Stopwatch = new Stopwatch ();            Hashtable Stopwatch.start (); for (int i = 0; i < Datacount; i++) {_hashtable.            ADD ("Key" + i.tostring (), "Value" + i.tostring ()); } STOPWAtch.            Stop ();            Console.WriteLine ("Hashtable" + Datacount + "bar time elapsed (milliseconds):" + stopwatch.elapsedmilliseconds);            Dictionary Stopwatch.reset ();            Stopwatch.start (); for (int i = 0; i < Datacount; i++) {_dictionary.            ADD ("Key" + i.tostring (), "Value" + i.tostring ());            } stopwatch.stop ();            Console.WriteLine ("Dictionary" + Datacount + "bar time elapsed (milliseconds):" + stopwatch.elapsedmilliseconds);            Concurrentdictionary Stopwatch.reset ();            Stopwatch.start (); for (int i = 0; i < Datacount; i++) {_condictionary.tryadd ("key" + i.tostring (), "Value" + I .            ToString ());            } stopwatch.stop ();            Console.WriteLine ("Concurrentdictionary" + Datacount + "bar time elapsed (milliseconds):" + stopwatch.elapsedmilliseconds);            Hashtable Stopwatch.reset ();            Stopwatch.start (); for (int i = 0; i < _hasHtable. Count;            i++) {var key = _hashtable[i];            } stopwatch.stop ();            Console.WriteLine ("Hashtable Traversal Time (ms):" + stopwatch.elapsedmilliseconds);            Dictionary Stopwatch.reset ();            Stopwatch.start (); for (int i = 0; i < _hashtable. Count;            i++) {var key = _dictionary["key" + i.tostring ()];            } stopwatch.stop ();            Console.WriteLine ("Dictionary Traversal Time (ms):" + stopwatch.elapsedmilliseconds);            Concurrentdictionary Stopwatch.reset ();            Stopwatch.start (); for (int i = 0; i < _hashtable. Count;            i++) {var key = _condictionary["key" + i.tostring ()];            } stopwatch.stop ();        Console.WriteLine ("Concurrentdictionary Traversal Time (ms):" + stopwatch.elapsedmilliseconds); }    }}

  

Final Summary:

Big Data Insertion Dictionary take the least time

Traverse Hashtable fastest is the 1/5,concurrentdictionary of the dictionary 1/10

Single threaded recommendations with dictionary, multithreading recommendations with Concurrentdictionary or Hashtable (Hashtable tab = hashtable.synchronized (New Hashtable ()); Get thread-safe objects)

C # Dictionary Collection Hashtable, Dictionary, concurrentdictionary three differences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.