Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: A memory efficient hash, trading off speed - does it already exist?

by steves (Curate)
on Feb 03, 2003 at 22:33 UTC ( [id://232396]=note: print w/replies, xml ) Need Help??


in reply to A memory efficient hash, trading off speed - does it already exist?

Getting back to your basic question: Can you avoid hash preallocation? Well, not really ... the hash has to be allocated to something. But you can allocate your own buckets by assigning to the keys function like this:

keys(%hash) = 512;
There are caveats -- Perl will round up to a power of 2 if needed.

As far as I know, Perl maintains the initial hash allocation and does not reallocate along the way, but I may be wrong about that.

There is no way to really use "just what you need" in a hash as you suggest ... The design of a hash guarantees that there will almost always be some unused slots unless you have a perfect hashing algorithm. That would be called an array. 8-)

  • Comment on Re: A memory efficient hash, trading off speed - does it already exist?
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://232396]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found