Just some measurements on .NET 4 for whole keys.
This is not quite the answer to your question, but for completeness, I measured the behavior of various classes useful for incrementing integers based on integer keys: simple Array , Dictionary (@Ani approach), Dictionary (simple approach), SortedDictionary (@Ani approach) and ConcurrentDictionary.TryAddOrUpdate .
Here are the results, adjusted to 2.5 ns for class packaging instead of direct use:
Array 2.5 ns/inc Dictionary (@Ani) 27.5 ns/inc Dictionary (Simple) 37.4 ns/inc SortedDictionary 192.5 ns/inc ConcurrentDictionary 79.7 ns/inc
And what a code .
Note that ConcurrentDictionary.TryAddOrUpdate is three times slower than Dictionary TryGetValue + indexer installer. And the latter is ten times slower than Array.
So, I would use an array if I knew that the key range is small, and the combined approach would otherwise.
tsul Jan 24 '17 at 12:50 2017-01-24 12:50
source share