<?xml version="1.0" encoding="windows-1252"?>
<node id="575970" title="Re^3: shift vs @_" created="2006-10-02 18:14:30" updated="2006-10-02 14:14:30">
<type id="11">
note</type>
<author id="190016">
sgifford</author>
<data>
<field name="doctext">
They seem to be about the same.  &lt;code&gt;shift&lt;/code&gt; comes out slightly ahead in this test, perhaps because it simplifies the loop.
&lt;code&gt;
#!/usr/bin/perl

use warnings;
use strict;

use Benchmark;

timethese(1_000_000, {
  'use_shift' =&gt; sub { sub_with_shift(0..9) },
  'use_list' =&gt; sub { sub_with_list(0..9) },
  'use_direct' =&gt; sub { sub_with_direct(0..9) },
});

sub sub_with_shift
{
  my $sum = 0;
  while (@_)
  {
    $sum += shift;
  }
  $sum;
}

sub sub_with_list
{
  my(@a)=@_;
  my $sum = 0;
  $sum += $_
    for @a;
  $sum;
}

sub sub_with_direct
{
  my $sum = 0;
  $sum += $_
    for @_;
  $sum;
}
&lt;/code&gt;
&lt;pre&gt;
Benchmark: timing 1000000 iterations of use_direct, use_list, use_shift...
use_direct:  7 wallclock secs ( 6.48 usr + -0.01 sys =  6.47 CPU) @ 154559.51/s (n=1000000)
  use_list: 10 wallclock secs ( 9.85 usr +  0.06 sys =  9.91 CPU) @ 100908.17/s (n=1000000)
 use_shift:  6 wallclock secs ( 6.48 usr +  0.01 sys =  6.49 CPU) @ 154083.20/s (n=1000000)
&lt;/pre&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-190016"&gt;
&lt;br&gt;
--&lt;br&gt;
&lt;i&gt;[http://www.suspectclass.com/~sgifford/|sgifford's Web page]&lt;/i&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
575918</field>
<field name="parent_node">
575956</field>
</data>
</node>
