C#hashtable and Dictionary Performance

Source: Internet
Author: User

Because Hashtable and Dictionary exist at the same time, there must be selectivity in the use of the scene, and not at any time can replace each other.
[1] Dictionary is recommended in single-threaded applications, with a generic advantage and a faster reading speed and fuller capacity utilization.
[2] The recommended use of Hashtable in multithreaded programs, the default Hashtable allow single-threaded write, multi-threaded read, Hashtable further calls to the Synchronized () method can obtain a fully thread-safe type. While Dictionary is non-thread-safe, it must be protected by the use of the lock statement, and the efficiency is greatly reduced.
[3] Dictionary has the characteristics of the data in the order of insertion (note: But when the call to remove () Delete the feast order is disrupted), so in the context of the need to reflect the order of the use of Dictionary can be a certain convenience.

Several C # frameworks provide data structures that compare the efficiency of single-value lookups->http://www.cnblogs.com/eaglet/archive/2008/10/23/1317893.html
I personally feel that whenever you should use DICTIONARY<K,V>, for the following reasons:
1. DIC is type-safe, which helps us write more robust and readable code, and saves us the hassle of forcing conversions. This is believed to be understood by everyone.
2, DIC is a generic line, when K or V is a value type, its speed is far more than Hashtable. This will also be understood if you understand the value type and the reference type.
3, if both K and V are reference types, as measured by eaglet, Hashtable is faster than DIC, here I would like to point out that the eaglet test is problematic. The reason is that Hashtable and dic use different data structures. Eaglet's "Dictionary is not right because it encapsulates a layer on the basis of Hashtable."

Dictionary uses the ContainsKey method to test for the existence of a key before calling the Add method, otherwise a keynotfoundexception is obtained.
When a program frequently tries a key that does not exist in the dictionary, the TryGetValue method is used to retrieve the value, which is a more efficient way to retrieve the value.

Specifically I do not speak, because someone (Angel Lucifer) has made it very clear that the quote is as follows:

Http://www.cnblogs.com/lucifer1982/archive/2008/06/18/1224319.html
Http://www.cnblogs.com/lucifer1982/archive/2008/07/03/1234431.html

Hashtable when specifying the capacity parameter, it does not only open the memory space of the capacity slot, but rather the space of the smallest prime slot that is larger than the capacity/0.72 (default filling factor), and DIC, when specifying capacity, is The space of the smallest prime number of slots larger than a capacity. So you can see, although the landlord is designated capacity for 100,000, and actually hashtable the total number of slots is far greater than the total number of DIC, that is, the memory is much larger than DIC, so testing is unfair injustice, such as to fair and impartial testing, The capacity of DIC should be specified as 100,000/0.72, please test its performance again.


Url:http://www.cnblogs.com/jhh0111/archive/2008/10/23/1318223.html

C#hashtable and Dictionary Performance

Related Article

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.