Link to home
Start Free TrialLog in
Avatar of akashchopra
akashchopra

asked on

Faster Alternative to Hashtable

I've been trying to use a Hashtable or a ListDictionary to store name/value pairs, where the value is of type "single".

Now either method works, but the problem is that I use the values in calculations, and lose a lot of performance because the Hashtable/ListDictionary stores the value as an object, and I have to convert to single before doing my calculation.

I've tried creating my own CustomHashtable that inherits from Hashtable, but that doesn't improve things because it is essentially adds strong typing on top of the existing Hashtable code, and internally still ends up converting to single.

What I think I need is a Hashtable implementation written from scratch that natively stores the values as singles. Does anyone know if any such class exists (full points to anyone who can provide such a class), or do I have to try and write one myself?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of purpleblob
purpleblob

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of akashchopra
akashchopra

ASKER

I've been investigating this some more, and I think the man performance hit comes from accessing the value, rather than converting it from object to single.

I replaced the hashtable with a simple array of objects, and it was much faster than than using the hashtable. In other words accessing myArray(5) is significantly faster than accessing myHash("red"). The problem of course is that the code is much harder to maintain and use, and I would much prefer to access values by key rather than index.

_TAD_: you suggested using an array of structs, but I'm guessing that unless I access the array by index it will still be slow (I'll have to implement a search for the key that I want).

ThePostMaster: I tried your idea of creating a wrapper class, but accessing mHashTable.Item(0).Value (as per your example), still involves a conversion between object and SingleWrapper, and is no faster than a plain old hashtable.

siqubal: Thanks for posting your implementation,but I don't see how it is any faster than a hashtable - maybe I'm missing something?

purpleblob: thanks for explaining that - doesn't seem like a solution will be available for some time.


So, after all that, I'm still stuck. I've tries reducing the load factor of the hashtable to make it faster, but the effect is minimal.

I'm going to leave this question open for another day or two, to see if anyone else suggests anything. If not, then I'll split the points between the responses so far.

Thanks for your comments so far.
No further comments, so I've split the points between all of you. Hope you find this acceptable!

Akash
Hmmm... If it's the lookup that's taking time, then you're probably out of luck. The hashtable will use a binary search, and there <i>simply ain't anything faster</i>. As a last resort, you could write your own typed hashtable and binary search from scratch, and that would get rid of the conversion issue.

Sorry I didn't comment sooner, I wasn't receiving e-mail because my mailbox was jammed full of SWEN virus e-mails.