RE: Hash table & threading



Hash Tables use more memory, but in almost every case a Hash Table will have
better performance than an array. The only case where it's best to simply use
an array is when the keys are all consecutive integers.
Hash Tables are roughly O(1) to access a value with its key, while arrays
are usually O(N) or similar depending on your algorithm for lookup.

If you want to compromise to save memory, one thing you can do is use a
tree-based map. That will have slower access time [usually O(log N)] but it
will use very low memory overhead.

Another way to lower memory usage is to set the load factor of your hash
table to a high value. Lower load factors mean faster lookup, but they also
mean more memory. In general, the memory a hash table uses is 1.5 /
load-factor * number-of-entries. Setting your load factor too high can cause
your hash table to perform like an array. Setting it too low can cause it to
resize too often. It is usually good to keep it around 0.7. Unfortunately the
Hashtable class provided in the .NET Platform doesn't have an easy way to
change load factor that I know of. I would just rely on the Hashtable
implementation to handle your data efficiently.

There is almost no case where the memory savings of an Array will make up
for the performance hit. It's almost certainly better to use a Hash Table.

"Ravi" wrote:

> Hi,
> I am working on a winform app. I need to use an object which can store
> some information(key/value pairs) and also can be acessed by multiple
> threads(read/write). From what I heard Hash table is best suited for it. My
> question is
> Why hash table. why can't we use an array.
> I thought hash table uses more memory than array. Correct me if am wrong.
>
>
.



Relevant Pages

  • Re: hash table size
    ... talking about chess programming ideas and what they do rather than actually ... you can clear out the memory. ... Just a couple cycles per hash check. ... The amount of physical memory each user gets is likely to be somewhat ...
    (rec.games.chess.computer)
  • Re: Perl hashing speedup ?
    ... Is it necessary to load all the data in memory at once? ... For me it is necessary to load all data. ... That is the reason why I am using hash. ... such hashes at a time) with approx. ...
    (comp.lang.perl.moderated)
  • Re: hash table size
    ... Change all the hash keys so you get a new random value. ... This way old entries from the previous search (or uninitialized hash memory) ... It is true that doing an AND is faster than a modulo. ... If you are runing a chess program for a reason, ...
    (rec.games.chess.computer)
  • Re: Firewall vs. IPS - Differences now (ISS, Intrushield 2.1?)
    ... > You risk running out of memory. ... That's like saying "it's trivial to DoS Aho-Corasic if you know the ... DoS's and improvements via use of the Jenkins hash are most illuminating. ... > replacement policy gives the worst behavior since an attacker can flood ...
    (Focus-IDS)
  • Re: [PATCH] tracing/kmemtrace: normalize the raw tracer event to the unified tracing API
    ... branch tracer needs that too for example. ... SLUB statistics and is only good for detecting allocation hotspots, ... to track would be the memory object itself. ... would be to do a static, limited size hash that could track up to N memory ...
    (Linux-Kernel)