Speed: Collection vs. Dictionary
Is there any difference in the speed of traversing a Collection versus a Dictionary? Or checking an item exists in a Collection versus checking it exists in a Dictionary? In both instances I'd be checking for a string value.
Thanks
-
It depends. Are those string values in the dictionary stored as key or as value?
0 -
As I only need to store the string values without a corresponding key or value the string values could be stored as either. As keys and values aren't ordered, is there a difference in speed?
0 -
There is a difference: List.Contains() is slower than Dictionary.ContainsKey(), unless the searched item happens to be the first one in the list.
I wrote a small test where I searched for the last item in a list of 5000. This resulted in ~6000 msec for 100,000 searches in a list, and 6 msec in a dictionary. A SortedSet<string> took ~300 msec.
So unless you have a very large list, or want to search many items, you will hardly find a difference, and you shoud pick the solution that is the most readable/maintainable.
0
Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.
Kommentare
3 Kommentare