Faster Hashtable lookups in .NET

mac2022-07-05  16

I almost forget where  the original source is from.

Hashtables are very convenient data structures however they are not very fast and it is very easy to make them even slower by using more logical, but ultimately much slower approach, consider the code below:

Hashtable oHash= Hashtable(); sKey=;oHash[sKey]=; sValue=; (oHash.Contains(sKey))sValue=()oHash[sKey];// clear up valuesValue=; oValue=oHash[sKey]; (oValue!=)sValue=()oValue;

The difference in performance is about 2 times: the reason is that checking whether a hashtable contains an item actually has the same or almost the same cost as retrieving value from it or getting null if it is not present: this would not work if possible value of a key can actually be null, but in many cases using faster version will improve performance nicely, so long as you are not bottlenecked elsewhere!

转载于:https://www.cnblogs.com/PeterWang/archive/2007/03/06/665053.html

最新回复(0)