SortedDictionary<K, V>
is the way to go. Not just because it is the right structure for your usage, but even performance-wise and maintenance-wise it will be just better.
I just want fast insertions
In the second case you will have to insert both into
Dictionary<K, V>
as well asSortedSet<K>
. That's two insertions (one O(1) and other O(log n)). I would expect it to be slower than single insertion to aSortedDictionary<K, V>
(O(log n)).SortedDictionary<K, V>
is internally implemented as aSortedSet<KeyValuePair<K, V>>
with comparison being done on theKey
part of theKeyValuePair<K, V>
. So if you're satisfied with the performance ofSortedSet<T>
, then there should be no looking back.
the sorteddictionary moves around just the keys (doubles), or both the keys and values
This is clearly micro-optimization. It's just a matter of moving around few extra bytes and that will hardly matter.
its not clear how sorteddictionary is internally implemented. Sortedset is red-black tree which is a proven performer.
SortedDictionary<K, V>
is internally implemented as a SortedSet<KeyValuePair<K, V>>
with comparison being done on the Key
part of the KeyValuePair<K, V>
. It is a red-black tree. So that is proven performer too...
Also note that SortedDictionary<K, V>
will be lighter on memory, as well as, results in faster removal as well as enumeration. The Dictionary<K, V>
/SortedSet<K>
hybrid approach will give you faster lookups, but it will have to do a lookup for each key in the dictionary for corresponding value part during enumeration. This is going to be slower.
Alert: I had not read your comments when I wrote the above !!
the struct I am using is kinda heavy ~100 bytes
If you can change it to class, do it. Moving around 100 bytes wont be nice if your app is performance critical one.
I made a quick and dirty Dictionary<K, V>
/SortedSet<K>
hybrid structure and tested it.
Indeed it was faster for a 100 byte struct when it came to insertion (more than twice as fast). Surely there is a penalty (who would create a 100 byte struct?).
When I changed it to class, they both gave the same insertion performance.
When I shrunk the size of the struct, even then the insertion performance was comparable.
So my suggestion is switch to a class and use SortedDictionary<K, V>
. If you are stuck with the struct, then Dictionary<K, V>
/SortedSet<K>
will be better. Good q, and +1.