Running the following code :
use strict;
use warnings;
use Benchmark;
my $defaultArray = [1,2,3,4];
sub VARIABLE {
my $arrayref = @_ ? $_[0] : $defaultArray;
return $arrayref->[0].$arrayref->[1].$arrayref->[2].$arrayref->[3];
}
Benchmark::cmpthese(
-10,
{
VARIABLE_DEFAULT => sub { VARIABLE() },
VARIABLE_NODEFAULT => sub { VARIABLE([4,3,2,1]) },
}
);
I get the following Benchmark results :
Rate VARIABLE_NODEFAULT VARIABLE_DEFAULT
VARIABLE_NODEFAULT 313631/s -- -74%
VARIABLE_DEFAULT 1210501/s 286% --
Why is the NODEFAULT version so much slower than the DEFAULT one ?