How to increase the search speed of hash tables with structure objects as keys

Source: Internet
Author: User

2009/07/13

    • Wanghaining
    • Reading: 81
    • Comment: 0
    • Recommended: 0
    • Captured: 0

Http://www.oobang.com/myNote/310 replication Link

RSS source: Translation-Recommended Daily excellent translations | imqiang

Translator: imqiang

When the structure object is used as the key of the hash table, the query operation of the hash table is poor. This is due to the internal gethashcode () method used for searching ().

If a structure contains only simple value types (INT, short, etc.),AlgorithmMost of the created hash values are placed in the same bucket.

For example, if a hash table creates 10 buckets, it is very likely that all keys will be placed in the same bucket. Therefore, when searching, the. NET runtime must traverse the entire bucket to obtain the value.

Bucket1-value1, value2, value3,..., valuen
Bucket2-(empty)
Bucket3-(empty)
Bucket4-(empty)
Bucket5-(empty)
Bucket6-(empty)
Bucket7-(empty)
Bucket8-(empty)
Bucket9-(empty)
Bucket10-(empty)

Therefore, on average, the time complexity of the search operation changes from O (1) to O (n ).

To overcome this problem, you can consider rewriting the gethashcode () method.

One way is to create a string, combine all the value types in the structure, and use a special character as the separator.

Because your structure is a query condition, you can ensure that the values of all structures are different, so that the generated strings are unique.

The generated string has a gethashcode () method, because it inherits from system. Object (like other objects) and only returns the output of this API.CodeInstances are easier to understand:

Struct
{
Int;
Short B;
Public struct (INT _ A, short _ B)
{
A = _;
B = _ B;
}
Public override int gethashcode ()
{
String hash = A. tostring () + ":" B. tostring ();
Return hash. gethashcode ();
}
}

The displayed production hashcode ensures that the hashcode is unique and thus enhances the search performance.

Reference: http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/cpref/html/frlrfsystemobjectclassgethashcodetopic. asp

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.