Short question: How many elements can be in a list to make a linear search like List O(n) faster then Dictionary which is O(1)?
I seem to remember in college(and high school) when comparing searching techniques ( binary vs linear) there was always a point where linear was faster. that was O(n) vs O(log)
I can’t draw the graph here. but there must be some over head for the constant performance. So the question is if I have 10 items does List.Find make more sense then
if (Dictionary.Contains(x))
value = Directiory[x]
Or a Hashtable where value = Hashtable[x] will not fail but will require boxing
I asked myself that same question, and ran a benchmark to find out. I found the speed of a dictionary already outperforms a list in lookup at around 3-4 elements.
That seemed far to few to me based on the overhead of a dictionary, so I checked around to see if anyone else came upon the same results, and that seems to be the results others found as well. http://dotnetperls.com/dictionary-time
That doesn’t mean, throw dozens of dictionaries into your code where they don’t make sense – there’s a memory overhead and construction time to deal with as well. But if you have a decently sized set of keyed data, take advantage of that structure.