home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomePerl CookbookSearch this book

5.13. Presizing a Hash


You want to preallocate memory for a hash to speed up your program so Perl won't have to incrementally allocate memory each time a new entry is added to the hash. Often you know the final size of a hash before you start building it up, and it's possible to use this information to speed up your program.


Assign the number of key-value pairs your hash will have to keys %HASH .

# presize %hash to $num
keys(%hash) = $num;


This new feature, first introduced in release 5.004 of Perl, may or may not improve your performance. Perl already shares keys between hashes, so if you already have a hash with "Apple" as a key, Perl won't need to allocate memory for another copy of "Apple" when you add an entry whose key is "Apple" to another hash.

# will have 512 users in %users
keys(%users) = 512;

Perl's internal data structures require the number of keys to be a power of 2. If we had said:

keys(%users) = 1000;

Perl would have internally allocated 1024 "buckets" for the hash. Keys and buckets aren't always one to one. You get the best performance when they are, but the distribution of keys to buckets is dependent on your keys and Perl's (immutable) hash algorithm.

See Also

The keys function in perlfunc (1) and Chapter 3 of Programming Perl ; Recipe 4.3

Previous: 5.12. Hashing References Perl Cookbook Next: 5.14. Finding the Most Common Anything
5.12. Hashing References Book Index 5.14. Finding the Most Common Anything

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.