Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://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.
--Stevie-O
$"=$,,$_=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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2024-04-18 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found