Skip to main content

Speed: Collection vs. Dictionary

Comments

3 comments

  • Berend Veldkamp

    It depends. Are those string values in the dictionary stored as key or as value?

    0
  • Permanently deleted user

    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
  • Berend Veldkamp

    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

Please sign in to leave a comment.