Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
One of the reasons why the arg version is so much faster is because of a little-known feature of Perl: the arguments to functions are passed by-reference. That means when you use $_[0], you're actually referring to the constant 'foobar' which is embedded in the Perl bytecode.

On the other hand, your $_ = 'foobar' induces a string copy of 'foobar' every single time that statement is executed. Since calculating the length is easy, the additional overhead of the extra copying *really* stands out.

I added these two subs to the benchmark:

sub xarg { my $tmp = $_[0]; length $tmp } sub xarg_shift { my $tmp = shift; length $tmp }
And reran it (dropping the time from 60s each to 10s each, because I'm far too impatient to wait six minutes for the test to rerun):
Benchmark: running arg, arg_shift, noarg, noarg2, xarg, xarg_shift for + at least 10 CPU seconds... arg: 10 wallclock secs (10.52 usr + -0.01 sys = 10.51 CPU) @ 48 +9653.09/s (n=5146254) arg_shift: 10 wallclock secs (10.28 usr + 0.00 sys = 10.28 CPU) @ 41 +4663.33/s (n=4262739) noarg: 9 wallclock secs (10.00 usr + 0.00 sys = 10.00 CPU) @ 28 +7455.40/s (n=2874554) noarg2: 11 wallclock secs (10.55 usr + 0.00 sys = 10.55 CPU) @ 28 +2535.73/s (n=2980752) xarg: 10 wallclock secs (10.59 usr + 0.00 sys = 10.59 CPU) @ 33 +7761.28/s (n=3576892) xarg_shift: 10 wallclock secs (10.39 usr + 0.01 sys = 10.40 CPU) @ 29 +7917.40/s (n=3098341) Rate noarg2 noarg xarg_shift xarg arg_shift + arg noarg2 282536/s -- -2% -5% -16% -32% + -42% noarg 287455/s 2% -- -4% -15% -31% + -41% xarg_shift 297917/s 5% 4% -- -12% -28% + -39% xarg 337761/s 20% 18% 13% -- -19% + -31% arg_shift 414663/s 47% 44% 39% 23% -- + -15% arg 489653/s 73% 70% 64% 45% 18% + --
Note that they're still not on equal footing: the additional symbol table lookup needed to find the location of $_ *really* hurts. This is because the work needed to find $_ in the symbol table is far greater than the work needed to find the length of it (because the length is precomputed).
But if you're worried about performance, keep in mind that your slowest one executed in an average of 5.5 microseconds, which on a short-running script isn't terrible.
$"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc

In reply to Re: $_ vs. argument passing by Stevie-O
in thread $_ vs. argument passing by Dylan

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (10)
    As of 2018-06-19 07:40 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (111 votes). Check out past polls.