C # Hashtable vs Dictionary Learning Notes

Source: Internet
Author: User

Hashtable and Dictionary are stored in key-value pairs, and I understand that Dictionary is a generic implementation of Hashtable.

The keys and values of the Hashtable are of type object. Therefore, both key and value can be different types of values. When the variable is defined as Dictionary<object, object> dic behaves as if it were hashtable.

classProgram {Static voidMain (string[] args) {Hashtable HT=NewHashtable (); Dictionary<Object,Object> dic =Newdictionary<Object,Object>(); TestClass TC1=NewTestClass (); TestClass TC2=NewTestClass (); Ht.             ADD (TC1, TC1); Ht. ADD ("Key2","HTV2"); Dic.            ADD (TC1, TC1); Dic. ADD ("Key2","Dicv2"); Console.WriteLine (HT[TC1]+"  "+ ht["Key2"]); Console.WriteLine (DIC[TC1]+"  "+ dic["Key2"]);        Console.ReadLine (); }        classTestClass { Public intx;  Public inty; }    }

The output is as follows:

Next, compare the performance aspects:

The first test is that both key and value are integral types:

Static voidMain (string[] args) {Hashtable HT=NewHashtable (); Dictionary<Object,Object> dic =Newdictionary<Object,Object>(); //dictionary<object, int> dic = new Dictionary<object, int> (); //dictionary<int, int> dic = new Dictionary<int, int> ();Stopwatch SW =NewStopwatch ();
intCount =1000000; Sw. Start (); for(inti =0; I < count; i++) {ht. ADD (i, i);} SW. Stop (); Console.WriteLine (SW. Elapsedmilliseconds);
//SW. Start (); //for (int i = 0; i < count; i++)//{ //DiC. ADD (i, I); //} //SW. Stop (); //Console.WriteLine (SW. Elapsedmilliseconds);Console.ReadLine (); }

I started with two paragraphs, and then I swapped two for loop code blocks to find that the test sequencing affects the test results. So I used a scheme to measure multiple values separately.

The printing results are as follows:

HASHTABLE:190 179 178 185 172

dictionary<object,object>: 184 192 177 184 195

Dictionarty<object,int>: 88 96 87 91 90

Dictionarty<int,int>:

It will be found that the more accurate the type of key value of the dictionary, the higher the performance.

  Summarize:

1, Hashtable will turn the key value into an object storage(packing and unpacking to consume performance)

2, unless the type of key and value are not unified, otherwise, do not use Hashtable;

3. The relationship between Hashtable and dictionary is like the relationship between ArrayList and list. (Epiphany)

C # Hashtable vs Dictionary Learning Notes

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.