lWe all know that when we want to find one item in a collection based on some key value that Dictionary/Hashset etc are the fastest options available in C#. But, obviously they rely on setting up buckets and calling a hash function on any key value used as an argument for a lookup - both of which have some overhead.
So by rights this must mean that - for a collection up to a certain size - looping over each item in a list/array looking for a match by "brute force" must be quicker (i.e. the List.Contains method)
There's an article at http://www.dotnetperls.com/dictionary-time that suggests this threshhold is three items. Frankly I'm surprised that a Dictionary performs better with so few items!
I'm curious as to whether any of you out there have done your own benchmarks and can verify this. I'm also curious about the time required to instantiate the Dictionary and List - which the article above has left out (and frankly in most of the insert-light/read-heavy situations we'd use a dictionary for it's probably irrelevant - but in some cases this could be an important factor in deciding which to use).
Also: if this is the case (and a Dictionary really is a better choice than a List with four or more values) then why is it so? The example benchmarked in the article uses string keys - is there a much bigger performance cost to the default string equality operator/IEquatable implementation than I realise? Does a Dictionary always call on the key's IEquatable implementation during a lookup - or only in the case of a hash collision?
And finally: would this threshhold of three items be much different if the type of the key were something with a simpler equality test (like an Int32/Int64/Guid)?