According to http://msdn.microsoft.com/en-us/library/ms379571(v=vs.80).aspx, a Hash table is the fastest data structure in C# when you know the number of records you are going to store because of the collision resulution method.
The better performance of a Hshtable over a Dictionary is doubtful however, due to boxing/unboxing which occurs when using a HashTable (http://msdn.microsoft.com/en-us/library/4yh14awz(v=vs.80).aspx).
Some sources say that HastTable is simply used for backward compability, and the new programs should always use a Dictionary.
My approach will be to use a Dictionary when I have to search for records and a List when I simply add new records or process records one by one. I will be curious to test the performance of a HastTable over a Dictionary though for a fixed-size array.
If you can measure the cost of boxing / unboxing then I will believe it. As far as I can tell with .net 2.0 and .net 4.0 there is no measurable cost of boxing / unboxing even if you do it 10,000 times.
ReplyDeleteI never got any of the benchmarks to show a difference, if you have gotten
More about....Dictionary Vs Hashtable
ReplyDeleteLing