use Benchmark qw(cmpthese);
use 5.010;
cmpthese(-2, {
shift => sub {
my @array = (1..10)x10000;
my $item = shift @array;
},
index => sub {
my @array = (1..10)x10000;
my $item = $array[0];
},
});
cmpthese(-2, {
inside => sub {
my $x = 0;
while ($x < 5) {
my $val = $x + 2; #repeatedly declaring a new variable?
say $val;
$x++;
}
},
outside => sub {
my ($x, $val);
$x = 0;
while ($x < 5) {
$val = $x + 2; #same old variable every time
say $val;
$x++;
}
},
});
####
Rate index shift
index 62.6/s -- -1%
shift 63.1/s 1% --
####
Rate inside outside
inside 132994/s -- -9%
outside 146050/s 10% --