The first is moderately faster from what I know, and Benchmark agrees.
use Benchmark 'cmpthese'; cmpthese -1, { 'shift'=> sub { my $x=shift; my $y=shift; my $z=shift; return }, 'assign'=> sub { my ($x,$y,$z)=@_; }, }; __END__ Benchmark: running assign, shift, each for at least 1 CPU seconds... assign: 2 wallclock secs ( 1.23 usr) @ 505470.78/s (n=622740) shift: 3 wallclock secs ( 1.01 usr) @ 435373.52/s (n=440598) Rate shift assign shift 435374/s -- -14% assign 505471/s 16% --
But when I add some even rudimentary code to the subs, like
for (1..100) { ++$x; $x+=$y+=$z }
the difference gets swamped to the point of being noise.
Benchmark: running assign, shift, each for at least 1 CPU seconds... assign: 2 wallclock secs ( 1.08 usr ) @ 7097.97/s (n=7680) shift: 1 wallclock secs ( 1.02 usr ) @ 6973.56/s (n=7120) Rate shift assign shift 6974/s -- -2% assign 7098/s 2% -- Benchmark: running assign, shift, each for at least 1 CPU seconds... assign: 2 wallclock secs ( 1.01 usr ) @ 7587.94/s (n=7679) shift: 2 wallclock secs ( 1.08 usr ) @ 7592.04/s (n=8207) Rate assign shift assign 7588/s -- -0% shift 7592/s 0% --
Go ahead and make the change, but Id be quite suprised if benchmark showed much difference.

You may need to make the function inline to see any real gains. Other tricks like using static variables (reduces allocation overhead but renders the code not thread safe). You may need to consider a lot of things. Poorly constructed regexes can chew up a lot of time. Without seeing the code and its usage theres no way anyone here could make any specific recommendations. :-)

You should have a look at When perl is not quite fast enough

HTH

--- demerphq
my friends call me, usually because I'm late....


In reply to Re: Silly question about function args by demerphq
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.