Reputation: 10444
The hash of an Int32
is the value of the Int32
via:
Hashtables (Dictionary etc) with integer keys
As such, what value is added by using a Dictionary(Of Integer, someObject)
(or any hashing collection, for that matter)?
I will, of course, need to use .Contains(integerKey)
for either to prevent errors... but I can skip the hashing algorithm altogether, right?
What type would you use to optimize insertion/retrieval?
EDIT: I expect that I may perform on the order of 10^5 lookups and 10^3 insertions, and these operations are certainly not the bottleneck of my process.
Upvotes: 1
Views: 185
Reputation: 1504022
Unless the numbers form a range 0...x (in which case you could just use a List<T>
or even just an array) I would still go for the Dictionary<int, Whatever>
approach. It's simple, it works, and it's almost certainly going to perform fast enough for you.
This really sounds like micro-optimization which ought to be skipped until you've proved that you've got a problem. How often are you going to be looking up items in the dictionary, compared with other operations?
EDIT: As Timwi says, there are indeed potential savings to be made here if this is really performance-critical. Without a generic key type and the virtual method calls to fetch hash codes and compare values, you could certainly do better. But I wouldn't trust any third-party collection as much as the built-in ones, and I certainly wouldn't trust my own collection implementations for anything non-trivial without a huge amount of testing... it would have to be a really significant bottleneck in the application as a whole before I considered moving away from the built-in types.
Upvotes: 4
Reputation: 64557
The Dictionary will just call GetHashCode
on the type, so for Int32 I would imagine this to be pretty quick. Basically, I think it's already optimised enough for you.
What type I use depends on the type of the key to the value, I tend not to worry about performance in most uses.
Upvotes: 2