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