#! perl -slw use strict; use Benchmark qw[ cmpthese ]; our $K ||= 8; our $N ||= 1000; our $M ||= 100; our $CHUNK ||= 80; our $chunk = ' ' x $CHUNK; our $BUFFER = ' ' x ( $M * $CHUNK ); cmpthese( -10, { std => q[ my %hash; for my $key ( 1 .. $N ) { $hash{ $key } = ''; for my $chunk ( 1 .. $M ) { $hash{ $key } .= $chunk; } } ], prealloc => q[ my %hash; for my $key ( 1 .. $N ) { $hash{ $key } = ' ' x ( $M * $CHUNK ); $hash{ $key } = ''; for my $chunk ( 1 .. $M ) { $hash{ $key } .= $chunk; } } ], gbuf => q[ my %hash; for my $key ( 1 .. $N ) { $BUFFER = ''; for my $chunk ( 1 .. $M ) { $BUFFER .= $chunk; } $hash{ $key } = $BUFFER; } ], lbuf => q[ my %hash; my $buffer = ' ' x ( $M * $CHUNK ); for my $key ( 1 .. $N ) { $buffer = ''; for my $chunk ( 1 .. $M ) { $buffer .= $chunk; } $hash{ $key } = $buffer; } ], gbuf_keys => q[ my %hash; keys %hash = $K; for my $key ( 1 .. $N ) { $BUFFER = ''; for my $chunk ( 1 .. $M ) { $BUFFER .= $chunk; } $hash{ $key } = $BUFFER; } ], lbuf_keys => q[ my %hash; keys %hash = $K; for my $key ( 1 .. $N ) { $buffer = ''; for my $chunk ( 1 .. $M ) { $buffer .= $chunk; } $hash{ $key } = $buffer; } ], }); __END__ P:\test>369770 -N=10000 -K=16384 Rate std prealloc lbuf gbuf lbuf_keys gbuf_keys std 0.858/s -- -16% -56% -56% -57% -57% prealloc 1.03/s 20% -- -48% -48% -48% -49% lbuf 1.96/s 128% 91% -- -0% -1% -2% gbuf 1.96/s 129% 91% 0% -- -1% -2% lbuf_keys 1.98/s 131% 93% 1% 1% -- -1% gbuf_keys 2.00/s 133% 95% 2% 2% 1% -- P:\test>369770 -N=20000 -K=16384 s/iter std prealloc gbuf_keys lbuf_keys lbuf gbuf std 2.36 -- -15% -50% -50% -53% -53% prealloc 2.00 18% -- -41% -42% -44% -45% gbuf_keys 1.18 100% 70% -- -1% -6% -6% lbuf_keys 1.17 102% 71% 1% -- -5% -6% lbuf 1.11 112% 80% 6% 5% -- -1% gbuf 1.10 114% 81% 7% 6% 1% -- P:\test>369770 -N=20000 -K=32678 s/iter std prealloc lbuf gbuf lbuf_keys gbuf_keys std 2.36 -- -14% -53% -54% -54% -54% prealloc 2.02 17% -- -45% -46% -46% -46% lbuf 1.11 111% 81% -- -2% -2% -3% gbuf 1.10 115% 84% 2% -- -0% -1% lbuf_keys 1.09 116% 85% 2% 0% -- -1% gbuf_keys 1.08 118% 86% 3% 1% 1% -- P:\test>369770 -N=10000 -K=16384 -M=200 Rate std prealloc lbuf lbuf_keys gbuf_keys gbuf std 0.435/s -- -25% -60% -60% -60% -61% prealloc 0.578/s 33% -- -47% -47% -47% -48% lbuf 1.09/s 152% 89% -- -0% -0% -1% lbuf_keys 1.10/s 152% 90% 0% -- -0% -1% gbuf_keys 1.10/s 153% 90% 0% 0% -- -1% gbuf 1.11/s 154% 91% 1% 1% 1% -- P:\test>369770 -N=10000 -K=16384 -M=300 (warning: too few iterations for a reliable count) s/iter std prealloc gbuf lbuf_keys gbuf_keys lbuf std 3.61 -- -28% -63% -64% -64% -64% prealloc 2.59 40% -- -49% -49% -50% -50% gbuf 1.32 173% 96% -- -1% -1% -2% lbuf_keys 1.31 175% 97% 1% -- -1% -2% gbuf_keys 1.30 177% 98% 1% 1% -- -1% lbuf 1.29 179% 100% 2% 2% 1% --