develooper Front page | perl.beginners | Postings from May 2012

Re: shift vs @_

Thread Previous | Thread Next
From:
Steve Bertrand
Date:
May 21, 2012 13:25
Subject:
Re: shift vs @_
Message ID:
4FBAA49E.2090804@gmail.com
On 2012-05-21 13:40, sono-io@fannullone.us wrote:
> On May 20, 2012, at 10:07 PM, David Christensen wrote:
>
>> I've updated function_arguments.pl with Benchmark, below. f_direct() is the fastest, f_shift() is in the middle (12% slower), and f_assign() is the slowest (37%).
>
> David,
>
> 	Are you saying that it would be faster to do:
>
> my $this_date = shift;
> my $output = shift;
>
> 	as opposed to:
>
> my ($this_date, $output) = @_;
>
> 	or am I not reading your assessment correctly?

Some tests:

           Rate shift  copy
shift 504541/s    --   -8%
copy  549451/s    9%    --

           Rate shift  copy
shift 559284/s    --   -6%
copy  592417/s    6%    --

The test code:

#!/usr/bin/perl

use warnings;
use strict;

use Benchmark qw(:all);

cmpthese ( 5000000, {
                 'copy'  => 'my @args = qw(a b c); copy(@args)',
                 'shift' => 'my @args = qw(a b c); shift_off(@args);',
         });

sub shift_off {
     my $x = shift;
     my $y = shift;
     my $z = shift;
}
sub copy {
     my ( $x, $y, $z ) = @_;
}

Steve

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About