Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
What's your opinion on the use of shift? Some have mentioned that during a code review, you can lose points for using it to extract function parameters. Others seem to be utterly infatuated with it, perhaps even to the point of perversion. No matter how useful I find it, there are still things that bother me, if only psychologically:
sub foo { return shift()->{foo}; }
Seems only a few operators away from fully-fledged obfuscation, but it works, no? That's the annoying part. The other annoying part is how well it works.


Out of the following, which one is the fastest?
package Foo; sub with_shift { my $self = shift; return $self->{foo}; } sub with_inline_shift { return shift()->{foo}; } sub with_index { return $_[0]->{foo}; } sub with_list { my ($self) = @_; return $self->{foo}; }
Ideally, which is a euphemism for "in that place where all is well and good, but you can't get there from here", the named parameter method would be the fastest. Unfortunately, this is just not the case. with_inline_shift() is always the fastest, regardless of the number of parameters passed. In fact, it is about 30% faster with one parameter, and at least 10% faster when loaded with 1000 parameters.

The performance curve of with_inline_shift is very similar to that of with_index, presumably because both of them have no local variables to declare. The other two, with_list and with_shift are similarly slower. my is pretty darned expensive, don't you think?

Is this a case of "nice guys finish last" or what?

In reply to Shift versus Sanity by tadman

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 avoiding work at the Monastery: (5)
As of 2024-04-24 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found