When accessing an item in a Dictionary, an exception is thrown if a non-existent key is passed. To avoid this, the typical thing to do is one of three solutions:
I decided to benchmark each solution to determine which has better performance. The consensus was that the
Try Catch Finally solution was going to be very slow compared to
TryGetValue, and that
ContainsKey will be slower than
TryGetValue. Let’s test this theory.
TryGetValue seems to be the most time-efficient solution for a Dictionary collection, and
Try Catch Finally is good if your project depends more on Exceptions for errors than it relies on being fast. Equivalent functions for other collection types may be analogous in speed.
If you want to know the approximate differences, read below.
My benchmark test recreated a dictionary every time the expected number of items increased during each iteration. I proceeded to iterate over the dictionary 1000 times per method, and averaged out the time it took per item.
The graph below is a logarithmic scale of the time it takes to access an item in a Dictionary, on average, versus the number of items in the collection.
What do all these lines mean? Somewhat conclusively that
Try Catch Finally and
TryGetValue are comparably faster than
If your project depends heavily on Exceptions for error handling, then that slight difference in speed between
TryGetValue probably doesn’t matter. Otherwise,
TryGetValue is the way to go!
This got me thinking though… What if I do in fact pass a key that isn’t present in my Dictionary? Does that effect performance whatsoever?
Here is the data for each of the methods, using only keys that don’t exist, on similar logarithmic scales:
The differences between Hit and Miss performance are negligible. So boring 🙁
So at least my previous statement about
Try Catch Finally still stands.