Hashtable Performance Comments


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Been a while style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA">since
I did a Q&A from internal discussion… Here goes, this one recently
went over the CLR Performance alias. style="mso-spacerun: yes">  "urn:schemas-microsoft-com:office:office" />


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Question:


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">We were planning to use Hashtable in
one of our projects but before we do that we had a question is that whether it
is efficient enough (in terms of performance and memory usage)  to use the
Hashtable class when the number of items in the hashtable may range from
100-200. For our Hashtable both the key and the value are going to be string
objects.


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Answer:


st1 ns = "urn:schemas-microsoft-com:office:smarttags" /> w:st="on">Anthony
Moore
,
Dev lead on the BCL team (who is also giving href="http://mymsevents.com/MyMSEvents/search.aspx?s=1&keywords=ARCL02&keywordtype=1&track=0&speaker=0&timeslot=0&future=0&submit=Search+Now%21">.NET
Framework: Tips and Tricks for Building Managed Components at the PDC)
responded:


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Hashtable should be very effective
with this range of items. The lookup should be approximately constant time.


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Hashtable has reasonable memory
overhead. If you care more about memory overhead than lookup speed, you can use
SortedList, which uses less memory but has O(LogN) looksups compared to O(1) for
Hashtable.


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


 

Comments (6)

  1. jef says:

    Wouldn’t be StringDictionary more suitable in this case?

  2. Brad Abrams says:

    StringDictionary is just a strongly-typed wrapper around Hashtable, and thus it would perform the same. There is certainly some usability gain in using strong typing but very, very little performance advantage.

  3. Julio Acevedo says:

    if the number of items in the collection may range from 4000-6000? which is the best option?

  4. Syed Hamid says:

    i want to use a data structure to store values used for developing UI components, I am not able to judge which data structure to use, either Hashtable, also i have some pre-criteria to retrieve values from the data strucutre in the order i have inserted them.. can i go ahead with hashtable and some other data structure which is more optimal in implementation.

  5. Daniel says:

    How much faster is the Hashtable or StringDictionary Class compared to VB’s Dictionary object?

  6. So the Hashtable class (and by extension, any Dictionary<TKey,TValue>) has O(1) performance at