I can do this one :o)
From perldoc perlfunc:
As an lvalue `keys' allows you to increase the
number of hash buckets allocated for the given
hash. This can gain you a measure of efficiency
if you know the hash is going to get big. (This
is similar to pre-extending an array by assigning
a larger number to $#array.) If you say
keys %hash = 200;
then `%hash' will have at least 200 buckets
allocated for it--256 of them, in fact, since it
rounds up to the next power of two. [...] | [reply] [d/l] [select] |
As pileswasp pointed out, you can preallocate your hash by assigning to keys, similar to preallocating an array by assigning to $#array.
Most of the time, of course, you'll just let Perl worry about the number of buckets. Here's how that works...
When you add a new key to the hash, perl checks whether there are too many keys for the number of buckets. If that's the case, perl allocates a bunch more buckets. Then, perl goes through the whole hash, calls the hash function for each key, and figures out where it belongs in the newly-resized hash.
You're probably thinking that resizing the hash and reinserting all the elements is slow; you're absolutely right. When perl has to resize a hash, it doubles the number of buckets, which keeps it from having to resize your hashes too often.
| [reply] |
Actually, Perl doesn't have to call the hash function for each key as the full hash value is stored along with the key so Perl just needs to compute the remainder between that value and the new number of buckets.
Another question is whether Perl decides to allocate more buckets based on the number of buckets in use or on the number of keys. I remember being surprised by the answer to this and vaguely remember that the answer has changed. Unfortunately I don't have the time right now to dig this information up.
-
tye
(but my friends call me "Tye")
| [reply] |
When you do have time I would be very interested to take a look.
| [reply] |