my $var = shift; # wrong
my($var) = @_; # right
sub foo { uc $_[0] } # OK
my($var1, $var2) = (shift, shift); # Um, no.
####
use Benchmark qw(cmpthese);
sub x_shift
{
while(@_)
{
my $a = shift;
}
}
sub x_copy1
{
for(my $i = 0; $i < $#_; $i++)
{
my $a = $_[$i];
}
}
sub x_copy2
{
my $end = $#_;
for(my $i = 0; $i < $end; $i++)
{
my $a = $_[$i];
}
}
my @args = (1 .. 100);
cmpthese(1000000,
{
x_shift => 'x_shift(@args)',
x_copy1 => 'x_copy1(@args)',
x_copy2 => 'x_copy2(@args)'
});
##
##
Rate x_copy2 x_copy1 x_shift
x_copy2 609756/s -- -18% -45%
x_copy1 746269/s 22% -- -32%
x_shift 1098901/s 80% 47% --
##
##
use Benchmark qw(cmpthese);
sub x_shift { my $a = shift }
sub x_copy { my $a = $_[0] }
my @args = (1 .. 3);
cmpthese(1000000, { x_shift => 'x_shift(@args)', x_copy => 'x_copy(@args)' });
##
##
Rate x_copy x_shift
x_copy 1298701/s -- -0%
x_shift 1298701/s 0% --