In-depth analysis of hashtable, Dictionary, sorteddictionary, sortedlist

Source: Internet
Author: User

Let's look at it first.Hashtable.

Msdn: a set of key/value pairs. These key/value pairs are organized according to the hash code of the key.

The hash algorithm converts any-length input (also called pre- ing and pre-image) into fixed-length output using the hash algorithm. The output is a hash value. This type of conversion is a compression ing, that is, the space of hash values is usually much smaller than the input space, and different inputs may be hashed into the same output, instead, it is impossible to uniquely determine the input value from the hash value.

A hashtable object consists of buckets that contain collection elements. A bucket is a virtual sub-group of each element in hashtable. Compared with searching and searching in most sets, a bucket can make searching and searching easier. Each bucket is associated with a hash code. The hash code is generated using a hash function and is based on the key of this element.

The default filling factor of the hashtable class is 1.0, but the default filling factor is 0.72. All the load factors input from the constructor are multiplied by 0.72 in the hashtable class. This is a demanding number. You may increase or decrease the filling factor by 0.01 at some time. Your hashtable access efficiency may increase or decrease by 50%, because the filling factor determines the size of the hash table, the size of the hash table affects the probability of key conflicts, which in turn affects the performance. 0.72 is a balanced value obtained by Microsoft through a large number of experiments.

Let's look at some source code of hashtable:

 

Hashtable. ctor

Hashtable expansion is an extremely time-consuming internal operation. The write efficiency is only 1/10 of the Read efficiency. Frequent expansion is a factor. When resizing, you need to create a new larger array in the hash, copy the content of the original array to the new array, and re-hash the array. How to new this larger array is also exquisite. The initial capacity of the hash is generally a prime number. When resizing, the size of the new array is set to a prime number of the same size as the original array.

 

The hashtable data structure has problems: the space utilization is low, the data is greatly affected by the fill factor, and all data needs to be re-hashed during resizing. Although Hash has O (1) data retrieval efficiency, its space overhead is usually large, in exchange for time. Therefore, hashtable is suitable for read operations that are frequent and have few write operations.

WhileDictionary <K, V>It also uses the hash algorithm to implement multiple chain structures through arrays. However, it adopts the separated link hash method. The separated link hashing method is not affected by the loading factor. During expansion, the original data does not need to be re-computed.

The dictionary <tkey, tvalue> using the separated link method maintains an array of linked lists internally. For this linked list array l0, L1,..., LM-1, the hash function will tell us where to insert element x into the linked list. Then, the find operation tells us which table contains x. The idea of this method is: although it is a linear operation to search for a linked list, if the table is small enough, the search is very fast (this is also true, and it is also a search, insert, delete and other operations are not always the cause of O (1 ). In particular, it is not restricted by the filling factor.
In this case, the common filling factor is 1.0. A lower filling factor does not significantly improve performance, but requires more extra space.

Dictionary Insertion Algorithm: 1. Calculate the hash value of the key and find the first index of the target bucket in buckets. 2. Check whether the key has been saved in sequence from the chain, 3. If not, determine whether freelist exists. 4. If freelist exists, remove the node from freelist and save the data; otherwise, append the data to the Count position.

The buckets array stores the beginning of all data links. Buckets [I] indicates the first element of the Data Link in bucket I. The entries struct array stores actual data and uses the next value as the backward index of the chain structure. The deleted data space will be stringed to the header of the freelist linked list. when data is inserted again, the freelist linked list will be searched first to improve the efficiency of searching the location of idle data items in entries. In the enumerator, the enumerated order is the ascending order of the subscript of the entries array.

WhileSorteddictionary, Msdn is described as follows:

Sorteddictionary <(of <(tkey, tvalue>) is a binary search tree with search complexity O (log n). n is the number of elements in the dictionary. In this regard, it correspondsSortedlist<(Of <(tkey, tvalue>) generic classes are similar. These two classes have similar object models and both have O (log n) Search complexity. The difference between these two classes is the memory usage and the speed at which elements are inserted and removed:

  1. Sortedlist <(of <(tkey, tvalue>) uses less memory than sorteddictionary <(of <(tkey, tvalue>.
  2. Sorteddictionary <(of <(tkey, tvalue>) allows you to perform faster insert and remove operations on unordered data. The time complexity of sorteddictionary is O (log n ), sortedlist <(of <(tkey, tvalue>) is O (n ).
  3. Sortedlist <(of <(tkey, tvalue>)> is faster than sorteddictionary <(of <(tkey, tvalue>) if the list is filled with sorted data at a time.

Sorteddictionary <K, V> is a K-ordered (K, v) data structure. It uses a red/black tree as its internal data structure to sort and save K.-treeset <t>, A red/black tree is a binary search tree. Each node has a black or red attribute. It has a better balance than a common Binary Search Tree. 2-4-4 is the data structure of the Red-black tree on the "theory.

2-3-4 Tree Insertion Algorithm: this algorithm is similar to the insertion of a binary search tree (inserting data to the leaf node of the tree). If the insertion position is 2-node or 3-node, it is directly inserted to the current node, if the insert position is 4-node, You need to split the current 4-node and then perform subsequent insert operations.

Let's test the insert and search performance of hashtable, dictionary, and sorteddictionary.

 

Performance test code

Final Result

 

(Original: http://www.cnblogs.com/moozi/archive/2010/05/23/1741980.html)

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.