Your benchmark is utterly flawed. You aren't measuring what you
think you are measuring.
- The arrays @empty and @prealloc are
lexical. This means, they are known in the file, and
only in the file. They cannot be accessed
from Benchmark.pm. After all, you are passing
in a string, not a coderef. The @empty and
@prealloc in the passed strings will be evalled
(in the main package) and will hence be package
variables (as they aren't my'ed).
- @::empty just grows and grows. Meaning you need
the allocate more and more memory. Of course it will be slower!
It will be enlightening to print the sizes of @::empty
and @::prealloc after the benchmark was run.
- You aren't measuring the influence of the pre-allocation at all.
Except for the very first run of "assign", the
@::empty array will contain at least $::size
elements.
- (q{data} x $::size) returns a single string, even in
list context. This explains why "slice" appears to be
much faster with strings than with numerals. The slice shouldn't
be a win as you state, because that forces Perl to build two
relatively large lists.
The following benchmark suggest that preallocating is actually a tad
bit
slower, and that using slices loses. Assigning is a little
faster than pushing.
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw /cmpthese timethese/;
our $size = 100_000;
cmpthese timethese (-10 => {
push => 'my @arr; push @arr => $_ for 0 .. $::size - 1',
assign => 'my @arr; $arr [$_] = $_ for 0 .. $::size - 1',
assignpre => 'my @arr; $#arr = $::size - 1;
$arr [$_] = $_ for 0 .. $::size - 1',
slice => 'my @arr; @arr [0 .. $::size - 1] = (0 .. $::size -
+1)',
slicepre => 'my @arr; $#arr = $::size - 1;
@arr [0 .. $::size - 1] = (0 .. $::size -
+1)',
} => 'none');
__END__
Rate slicepre slice push assignpre assign
slicepre 3.82/s -- -1% -37% -40% -41%
slice 3.87/s 1% -- -36% -39% -40%
push 6.08/s 59% 57% -- -4% -5%
assignpre 6.36/s 66% 64% 5% -- -1%
assign 6.44/s 68% 66% 6% 1% --
Abigail
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.