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.


In reply to Re: Does "preallocating hash improve performance"? Or "using a hash slice"? by dave_the_m
in thread Does "preallocating hash improve performance"? Or "using a hash slice"? by vr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":