my $str = shift || "17:43:33:21:23:19:27:6";
my @a1 = ();
my @a2 = ();
my @a = split /:/, $str;
while ( my ( $p, $ad ) = splice @a, 0, 2 ) {
push @a1, map {$ad} (1..$p);
push @a2, ($ad) x $p;
}
use Benchmark 'cmpthese';
print "Accessing element 0:\n";
cmpthese(-1, { made_by_map => sub { $a1[0]; },
made_by_x => sub { $a2[0]; } });
print "\nAccessing element 50:\n";
cmpthese(-1, { made_by_map => sub { $a1[50]; },
made_by_x => sub { $a2[50]; } });
print "\nAccessing element 99:\n";
cmpthese(-1, { made_by_map => sub { $a1[99]; },
made_by_x => sub { $a2[99]; } });
print "\nAccessing random element:\n";
cmpthese(-1, { made_by_map => sub { $a1[rand 100]; },
made_by_x => sub { $a2[rand 100]; } });
####
Accessing element 0:
Rate made_by_x made_by_map
made_by_x 7554581/s -- -11%
made_by_map 8479758/s 12% --
Accessing element 50:
Rate made_by_x made_by_map
made_by_x 7788171/s -- -4%
made_by_map 8095273/s 4% --
Accessing element 99:
Rate made_by_map made_by_x
made_by_map 8058884/s -- -9%
made_by_x 8882975/s 10% --
Accessing random element:
Rate made_by_map made_by_x
made_by_map 1724352/s -- -2%
made_by_x 1758653/s 2% --
####
--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/;
map{y/X_/\n /;print}map{pop@$_}@/for@/