You pays your money and takes your choice:^)

#! perl -slw use strict; use Benchmark qw[cmpthese]; use constant ARG1 => 0; use constant ARG2 => 1; use constant LAST => -1; sub directly{ return "$_[ARG1] - $_[ARG2]" if $_[LAST]; } sub shifty{ my ($arg1, $arg2, $arg3) = (shift, shift, shift); return " +$arg1 - $arg2" if $arg3; } sub listy{ my ($arg1, $arg2, @others) = @_; return "$arg1 - $arg2" if +$others[-1]; } sub listya{ my (@args) = @_; return "$args[1] - $args[1]" if $args[-1] +; } our @a100 = 1..100; cmpthese( -3, { directly3 => 'directly 1, "TWO", 3.0;', directly100 => 'directly @a100;', shifty3 => 'shifty 1, "TWO", 3.0;', shifty100 => 'shifty @a100;', listy3 => 'listy 1, "TWO", 3.0;', listya => 'listya 1, "TWO", 3.0', listya100 => 'listya @a100;', }); __END__ Name "main::a100" used only once: possible typo at C:\test\233863.pl l +ine 14. Benchmark: running directly100, directly3, listy3, listya, listya100, shifty100, shifty3 , each for at least 3 CPU seconds directly100: 5 wallclock secs ( 3.19 usr + 0.00 sys = 3.19 CPU) @ 2 +7542.27/s (n=87970) directly3: 1 wallclock secs ( 3.03 usr + 0.00 sys = 3.03 CPU) @ 4 +7871.13/s (n=145241) listy3: 4 wallclock secs ( 3.10 usr + 0.00 sys = 3.10 CPU) @ 1 +9624.88/s (n=60896) listya: 4 wallclock secs ( 3.04 usr + 0.00 sys = 3.04 CPU) @ 1 +9505.42/s (n=59394) listya100: 3 wallclock secs ( 3.12 usr + 0.00 sys = 3.12 CPU) @ 3 +179.52/s (n=9936) shifty100: 4 wallclock secs ( 3.04 usr + 0.00 sys = 3.04 CPU) @ 2 +1964.84/s (n=66839) shifty3: 5 wallclock secs ( 3.09 usr + 0.00 sys = 3.09 CPU) @ 3 +0639.55/s (n=94523) Rate listya100 listya listy3 shifty100 directly100 shif +ty3 directly3 listya100 3180/s -- -84% -84% -86% -88% - +90% -93% listya 19505/s 513% -- -1% -11% -29% - +36% -59% listy3 19625/s 517% 1% -- -11% -29% - +36% -59% shifty100 21965/s 591% 13% 12% -- -20% - +28% -54% directly100 27542/s 766% 41% 40% 25% -- - +10% -42% shifty3 30640/s 864% 57% 56% 39% 11% + -- -36% directly3 47871/s 1406% 145% 144% 118% 74% +56% --

Direct access is hands down fastest, with shift second and list assignment last. Directly accessing 100 args is nearly as fast as shifting 3!;

However, what if any discernable difference it makes will depend very much on how many times your calling the sub, how deeply the call stack grows and how much you are doing in the sub. Calling small subs with many parameters many times benefiting much more than a few calls to subs that do lots of work.


Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.


In reply to Re: Silly question about function args by BrowserUk
in thread Silly question about function args by Anonymous Monk

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.