ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
5.13. Presizing a HashProblemYou 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. SolutionAssign the number of key-value pairs your hash will have to # presize %hash to $num keys(%hash) = $num; DiscussionThis 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 # 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 AlsoThe |