Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I found this bit odd:
Do not use shift. Use @_. shift is slower, and Brian has an allergic reaction to it.
my $var = shift; # wrong my($var) = @_; # right sub foo { uc $_[0] } # OK my($var1, $var2) = (shift, shift); # Um, no.
I always use shift, and I recall benchmarking it way back in the day before I made that decision, just to make sure it wasn't slower. Here's the test I used, and the results:
use Benchmark qw(cmpthese); sub x_shift { while(@_) { my $a = shift; } } sub x_copy1 { for(my $i = 0; $i < $#_; $i++) { my $a = $_[$i]; } } sub x_copy2 { my $end = $#_; for(my $i = 0; $i < $end; $i++) { my $a = $_[$i]; } } my @args = (1 .. 100); cmpthese(1000000, { x_shift => 'x_shift(@args)', x_copy1 => 'x_copy1(@args)', x_copy2 => 'x_copy2(@args)' });
Rate x_copy2 x_copy1 x_shift x_copy2 609756/s -- -18% -45% x_copy1 746269/s 22% -- -32% x_shift 1098901/s 80% 47% --
Seeing the flat claim that shift is slower made me rethink my earlier test and try something more simple:
use Benchmark qw(cmpthese); sub x_shift { my $a = shift } sub x_copy { my $a = $_[0] } my @args = (1 .. 3); cmpthese(1000000, { x_shift => 'x_shift(@args)', x_copy => 'x_copy(@ar +gs)' });
Sure enough, the results changed, but now it appears to be a tie:
Rate x_copy x_shift x_copy 1298701/s -- -0% x_shift 1298701/s 0% --

The results fluctuate a few percent in each direction. Anyway, my point is that I don't think performance is a reason to avoid using shift.

(I have a whole directory of benchmarks for silly little things, e.g. benchmarking exists() vs. a test for truth on a hash key and crazy stuff like that. It's a disease...but it amuses me :-)


In reply to Re^2: I need perl coding standards by siracusa
in thread I need perl coding standards by kprasanna_79

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 musing on the Monastery: (3)
As of 2024-04-23 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found