[EMAIL PROTECTED] wrote:
I've been using keys() as an lvalue to preallocate memory for hashes,
and am suddenly questioning my own wisdom. From somewhere I got the
notion that a statement like
keys(%hash) = 98;
will set the size of %hash not to 98 but to 128. The reason being that
"when used in this manner, keys() rounds up to the next higher power of
two (1, 2, 4, 8...)," and 2**7 would be the next higher power of two for
98. Lately I'm questioning this, as it would lead to some ridiculously
large round-ups, quite quickly. To compound things, I can no longer find
the reference from which I got this notion.
Can anyone set me straight? Do I have the right of this, or was I dreaming?
Yes, it does round up. However, A) it only rounds up the number of
buckets; it doesn't "preallocate memory" for the whole hash; and B) the
rounding up will happen as you add items to the hash, anyway, so using
keys() won't use any more memory, unless (obviously!) you set it to too
large a number, whereas using keys() /will/ make your program a little
faster, because it won't have to, every time a power-of-2 line is
crossed, create a new hash, twice as big, copy the old hash into it, and
then delete the old hash.
--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
-- Charles Williams. "Judgement at Chelmsford"
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs