In this article, we will explain the construction of hashtable and dictionary from a basic perspective, and compare it with inserting and reading through a program.
I. hashtable
1. hashtable is a hash table that maintains many key-value pairs internally. Another index-like value is hashcode.GethashcodeObtain the key using a certain algorithm. All the search operations and positioning operations are based on the hash value to find the corresponding key and value.
2. We need to use an algorithm to ensure that the space addresses of hash values corresponding to hashtable are not repeated as much as possible. This is the hash function (Gethashcode.
3. When a hashtable occupies more than half of the resource, the address value obtained by calculating the hash value may point to the same address repeatedly. This is a hash conflict.
In. position = (hashcode & 0x7fffffff) % hashtable. length ,. in net, hash conflicts are solved through the probe method. When the position postion obtained by the hash value and the position is occupied, A displacement x value will be added to determine whether the next position postion + X is occupied. If it is still occupied, continue to shift X to determine whether the position + 2 * X position is occupied, if not, place the value in it. When the available space in hashtable is getting more and more hours, it is more and more difficult to obtain the available space, and more time is consumed.
4. When the occupied space in hashtable reaches a percentage, it is automatically resized. In. net, the percentage is 72%, also known as the hashtable fill factor of. NET is 0.72. For example, if the size of a hashtable is 100, the hashtable will be expanded when it needs to add 73rd values.
5. What is the automatic resizing size? The answer is the prime number closest to twice the size of the current space. For example, hashtable currently occupies the space of prime 71. If it is expanded, the size of expansion is 131.
Ii. Dictionary
1. dictionary is a variant of hashtable. It uses a data structure that separates the link hash to solve the hash conflict problem.
2. The separated link hash is used when values of the same address are hashed into a linked list.
3. the fill factor of hashtable is 1.
3: This article will explore the efficiency of hashtable and dictionary insertion and three reading methods in the form of code (for/foreach/getenumerator)
Public class hashtabletest {static hashtable _ hashtable; static dictionary <string, Object> _ dictionary; static void main () {compare (10); compare (10000); compare (5000000 ); console. readline ();} public static void compare (INT datacount) {console. writeline ("----------------------------------------------- \ n"); _ hashtable = new hashtable (); _ dictionary = new dictionary <string, Object> (); stopwatch = new stopwatch (); // It takes time for hashtable to insert datacount data records to stopwatch. start (); For (INT I = 0; I <datacount; I ++) {_ hashtable. add ("str" + I. tostring (), "value");} stopwatch. stop (); console. writeline ("hashtable Insert" + datacount + "data entry time required:" + stopwatch. elapsed); // it takes time for dictionary to insert datacount data records to stopwatch. reset (); stopwatch. start (); For (INT I = 0; I <datacount; I ++) {_ dictionary. add ("str" + I. tostring (), "value");} stopwatch. stop (); console. writeline ("dictionary Insert" + datacount + "time required for data entry:" + stopwatch. elapsed); // it takes time for dictionary to insert datacount data records to stopwatch. reset (); int Si = 0; stopwatch. start (); For (INT I = 0; I <_ hashtable. count; I ++) {Si ++;} stopwatch. stop (); console. writeline ("hashtable Traversal Time:" + stopwatch. elapsed + ", traversal using the for method"); // it takes time to insert datacount data into stopwatch. reset (); SI = 0; stopwatch. start (); foreach (VAR s in _ hashtable) {Si ++;} stopwatch. stop (); console. writeline ("hashtable Traversal Time:" + stopwatch. elapsed + ", using the foreach Method for traversal"); // it takes time to insert datacount data into stopwatch. reset (); SI = 0; stopwatch. start (); idictionaryenumerator _ hashenum = _ hashtable. getenumerator (); While (_ hashenum. movenext () {Si ++;} stopwatch. stop (); console. writeline ("hashtable Traversal Time:" + stopwatch. elapsed + ". hashtable is used for traversal. getenumerator () method "); // dictionary it takes time to insert datacount data records to stopwatch. reset (); SI = 0; stopwatch. start (); For (INT I = 0; I <_ dictionary. count; I ++) {Si ++;} stopwatch. stop (); console. writeline ("dictionary Traversal Time:" + stopwatch. elapsed + ", traversal using the for method"); // it takes time to insert datacount data into stopwatch. reset (); SI = 0; stopwatch. start (); foreach (VAR s in _ dictionary) {Si ++;} stopwatch. stop (); console. writeline ("dictionary Traversal Time:" + stopwatch. elapsed + ", using the foreach Method for traversal"); // it takes time to insert datacount data into stopwatch. reset (); SI = 0; stopwatch. start (); _ hashenum = _ dictionary. getenumerator (); While (_ hashenum. movenext () {Si ++;} stopwatch. stop (); console. writeline ("dictionary Traversal Time:" + stopwatch. elapsed + ", which uses dictionary for traversal. getenumerator () method "); console. writeline ("\ n -------------------------------------------------");}}
4. From the above results, we can see that
1. It takes more time for hashtable to insert large amounts of data than a dictionary.
2. The for method can traverse hashtable and dictionary at the fastest speed.
3. In the foreach mode, the speed of the dictionary traversal is faster.
5. It is better to use a dictionary in a single thread and hashtable in multiple threads.
Because hashtable can use hashtable tab = hashtable. synchronized (New hashtable (); to obtain thread-safe objects.
Of course, some errors may occur due to different computer situations. If you have any questions, please correct them.