ever wondered whether using a series of shift's or a list assignment at the start of a method/function is faster? well, i did. so i benchmarked: 1/3 shift(s); compared to a 1/3 argument assignment to a list; compared to using @_ directly. the function body itself is a trivial integer addition, so as not to detract from the point of the case study.

so, here are the results (re-formatted slightly for readability):

Benchmark: timing 2000000 iterations of argumentative_1arg, argumentat +ive_3args, direct_1arg, direct_3args, shifty_1arg, shifty_3args... argumentative_1arg: 16 wallclock secs (15.85 usr + 0.03 sys = 15.88 CPU) @ 125944.58/s argumentative_3args: 21 wallclock secs (19.45 usr + 0.01 sys = 19.46 CPU) @ 102774.92/s direct_1arg: 11 wallclock secs ( 9.75 usr + 0.01 sys = 9.76 CPU) @ 204918.03/s direct_3args: 12 wallclock secs (11.15 usr + 0.03 sys = 11.18 CPU) @ 178890.88/s shifty_1arg: 16 wallclock secs (16.44 usr + -0.01 sys = 16.43 CPU) @ 121728.55/s shifty_3args: 23 wallclock secs (21.91 usr + 0.02 sys = 21.93 CPU) @ 91199.27/s

in summary - there's bugger-all difference between using shifts and assigning to a list. using the contents of @_ directly is, naturally, faster, but not overwhelmingly so - certainly not enough to justify the potential obfuscation (and danger!) factor of using @_ directly.

here's the code that was used:

#!/usr/bin/perl -w use Benchmark; use strict; package ArgTest; sub shifty_1arg { my $this = shift; my $first_arg = shift; $$this += $first_arg; } sub shifty_3args { my $this = shift; my $first_arg = shift; my $second_arg = shift; my $third_arg = shift; $$this += $first_arg + $second_arg + $third_arg; } sub argumentative_1arg { my( $this, $first_arg ) = @_; $$this += $first_arg; } sub argumentative_3args { my( $this, $first_arg, $second_arg, $third_arg ) = @_; $$this += $first_arg + $second_arg + $third_arg; } sub direct_1arg { ${$_[0]} += $_[1]; } sub direct_3args { ${$_[0]} += $_[1] + $_[2] + $_[3]; } # main /------------------------------------------------ package main; my $total; bless( my $object = \$total, 'ArgTest' ); my @args = ( 1 .. 3 ); timethese( 100_000, { shifty_1arg => sub { $object->shifty_1arg( @args ) }, argumentative_1arg => sub { $object->argumentative_1arg( @ar +gs ) }, direct_1arg => sub { $object->direct_1arg( @args ) }, shifty_3args => sub { $object->shifty_3args( @args ) } +, argumentative_3args => sub { $object->argumentative_3args( @a +rgs ) }, direct_3args => sub { $object->direct_3args( @args ) } +, } ); print "total: $total\n"; exit;

In reply to benchmark for shift vs list assignment for object methods by d_i_r_t_y

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.