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
- The number are far too small to be meaningful, and one would expect the difference to grow as the hash size increases. (1 vs 2: 3%, 4%, 4%, 6%; 3 vs 4: -3%, 5%, 3%, 4%)
- Well, except on the first pass of a given size of a given test.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|