by Michel
1. June 2009 16:52
After reading the following blog post I became curious if I could reproduce the same behavior on the HashSet.
So what did I do. I tested an List, Dictionary and a HashSet.
For all types I ran the test five times. Every run I added 10.000 items. Besides adding items to the collection I checked if the collection contained the item. This test I did 5 times with 10.000 items in the collection to check.
The figure show the result for adding the items to the different collections, and checking if the collection contained the item.
The horizontal line shows the amount of milliseconds to Add or check if the item contains in the collection. Without having a look at the numbers it is obvious that the Contains method on the List collections is slower than the Contains method on the HashSet or the Containskey on a Dictionary. The difference can be explained because the HashSet collection as well as the Dictionary collection uses a hashing algorithm to quickly retrieve data from it’s collection.
To get an better view on the time it takes to add an item to the different collection and see which collection is faster for retrieving the data, I removed the data duration data from the List Contains method is the graph below.
The graph shows that the HashSet is slower to do an lookup than the Dictionary. To be precise, it took the double amount of time to check every single item if it was in the collection. For adding stuff the List collection was the absolute winner. If we compare both structures who uses the a hashing table, the HashSet was the absolute winner. It took the Dictionary object 37,5% longer to add 10.000 items.
Even Stian Sveen got a lot of complains about his post. I got exactly the same result as he did. Until somebody else proven otherwise I believe that a Dictionary collection is faster for retrieving items than a HashSet when the collection contains 10.000 items.
ac7c9f37-4ee6-4913-9fd6-7190afe256c1|2|5.0
Tags: