in reply to Re^2: Does "preallocating hash improve performance"? Or "using a hash slice"?
in thread Does "preallocating hash improve performance"? Or "using a hash slice"?
slicing does not seem to include the preallocation optimization
I don't know how you can say given that the test shows no benefit from pre-allocating[1].
But the reason the test shows no benefit from pre-allocating because the test is still flawed.
Lexical variables aren't freed when they go out of scope; they are kept around for use the next time the scope is entered. That means the hash is effectively pre-allocated for all tests![2].
$ perl -MDevel::Peek -e' sub x { my %h; Dump(%h, 0); keys(%h) = 100; Dump(%h, 0); } x() for 1..3; ' 2>&1 | grep MAX MAX = 7 MAX = 127 MAX = 127 <-- Preallocated even before C<< keys(%h) = 100; >>! MAX = 127 MAX = 127 <-- Preallocated even before C<< keys(%h) = 100; >>! MAX = 127
Adding undef %h; should provide better results.
$ perl -MDevel::Peek -e' sub x { my %h; Dump(%h, 0); keys(%h) = 100; Dump(%h, 0); undef %h; } x() for 1..3; ' 2>&1 | grep MAX MAX = 7 MAX = 127 MAX = 7 MAX = 127 MAX = 7 MAX = 127
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Does "preallocating hash improve performance"? Or "using a hash slice"?
by ikegami (Patriarch) on Feb 23, 2017 at 01:37 UTC |