Ian Robertson ([EMAIL PROTECTED]) wrote:
> >>>>> "AS" == Adam Spiers <[EMAIL PROTECTED]> writes:
>  AS> While we're at it, another quick poll:
> 
>  AS>   my $self = shift;
>  AS>   my $arg = shift;
> 
>  AS> or
> 
>  AS>   my ($self, $arg) = @_;
> 
>  AS> ?
> 
>  AS> IIRC some of my cow-orkers here benchmarked it and found that the
>  AS> latter was noticeably faster.
> 
> FWIW, on my box, the shift method seems to be a touch faster if there
> are one or two arguments (including $self); the @_ method starts to
> win at three.  However, the difference is in the fraction of a
> microsecond range, so it's probably not a deciding factor :).

That's odd, my tests show that the more shifts you use, the slower it
gets.  Try the attached benchmark.  It may be something to do with a
difference in Perl versions, of course.  Mine's 5.6.0.
#!/usr/bin/perl -w

use strict;

use Benchmark qw(cmpthese);

my $obj = bless {};

cmpthese(500000, {
  two_none  => sub { $obj->two_none(4) },
  two_one   => sub { $obj->two_one(4)  },
  two_all   => sub { $obj->two_all(4)  },
});

cmpthese(500000, {
  three_none  => sub { $obj->three_none(4, 'blah') },
  three_one   => sub { $obj->three_one(4, 'blah')  },
  three_all   => sub { $obj->three_all(4, 'blah')  },
});

cmpthese(500000, {
  four_none  => sub { $obj->four_none(4, 'blah', [ 'qux', 3 ]) },
  four_one   => sub { $obj->four_one(4, 'blah', [ 'qux', 3 ])  },
  four_all   => sub { $obj->four_all(4, 'blah', [ 'qux', 3 ])  },
});

cmpthese(500000, {
  four_none  => sub { $obj->four_none(4, [ 'qux', 3 ], 'blah') },
  four_one   => sub { $obj->four_one(4, [ 'qux', 3 ], 'blah')  },
  four_all   => sub { $obj->four_all(4, [ 'qux', 3 ], 'blah')  },
});

cmpthese(500000, {
  five_none  => sub { $obj->five_none([ 'qux', 3 ], 'blah', 5, 0) },
  five_one   => sub { $obj->five_one([ 'qux', 3 ], 'blah', 5, 0)  },
  five_all   => sub { $obj->five_all([ 'qux', 3 ], 'blah', 5, 0)  },
});

sub two_none {
  my ($self, $arg) = @_;
}

sub two_one {
  my $self = shift;
  my ($arg) = @_;
}

sub two_all {
  my $self = shift;
  my $arg = shift;
}

sub three_none {
  my ($self, $arg1, $arg2) = @_;
}

sub three_one {
  my $self = shift;
  my ($arg1, $arg2) = @_;
}

sub three_all {
  my $self = shift;
  my $arg1 = shift;
  my $arg2 = shift;
}

sub four_none {
  my ($self, $arg1, $arg2, $arg3) = @_;
}

sub four_one {
  my $self = shift;
  my ($arg1, $arg2, $arg3) = @_;
}

sub four_all {
  my $self = shift;
  my $arg1 = shift;
  my $arg2 = shift;
  my $arg3 = shift;
}

sub five_none {
  my ($self, $arg1, $arg2, $arg3, $arg4) = @_;
}

sub five_one {
  my $self = shift;
  my ($arg1, $arg2, $arg3, $arg4) = @_;
}

sub five_all {
  my $self = shift;
  my $arg1 = shift;
  my $arg2 = shift;
  my $arg3 = shift;
  my $arg4 = shift;
}

Reply via email to