So, are my tests flawed
Kinda. Your benchmarks are dominated by the time taken to generate @a and 'map log @a'. If these are precomputed you can start to see a big improvement with slicing and a smaller improvement with pre-sizing.
use strict;
use warnings;
use Benchmark qw/ cmpthese /;;
for my $count ( 100, 1_000, 10_000, 100_000 ) {
my @a = map { log } 2 .. $count;
my @b = map log @a;
cmpthese( -5, {
1 => sub {
my %h;
keys %h = @a;
$h{ $_ } = log for @a;
return \%h
},
2 => sub {
my %h;
$h{ $_ } = log for @a;
return \%h
},
3 => sub {
my %h;
keys %h = @a;
@h{ @a } = @b;
return \%h
},
4 => sub {
my %h;
@h{ @a } = @b;
return \%h
},
})
}
$ perl5240o ~/tmp/x.pl
Rate 2 1 4 3
2 14381/s -- -1% -25% -26%
1 14516/s 1% -- -24% -26%
4 19198/s 33% 32% -- -2%
3 19510/s 36% 34% 2% --
Rate 2 1 4 3
2 1427/s -- -4% -25% -29%
1 1480/s 4% -- -22% -26%
4 1895/s 33% 28% -- -5%
3 2004/s 40% 35% 6% --
Rate 2 1 4 3
2 131/s -- -6% -22% -29%
1 140/s 6% -- -17% -25%
4 168/s 28% 20% -- -9%
3 185/s 41% 33% 10% --
Rate 2 1 4 3
2 6.50/s -- -6% -12% -18%
1 6.94/s 7% -- -6% -12%
4 7.39/s 14% 6% -- -7%
3 7.91/s 22% 14% 7% --
$
Dave.
-
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.
|