Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
For subs use the content of the @_, specially for big data sent to the function:
Don't use:
sub { my ($var1,$var2) = @_ ; }
The best way is to use the @_[0] it self or the shift:
sub { my $var1 = shift ; my $var2 = shift ; }
* If you use @_[?] you can't modifie it! Use shift if you need to write to the var.

I was pretty sure that was wrong when I read it, so I whipped out Benchmark:

#!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); sub shifter { my $a=shift; my $b=shift; my $c=shift; my $d=shift; my $e=shift; my $f=shift; return $a*$b*$c*$d*$e*$f; } sub assigner { my ($a,$b,$c,$d,$e,$f)=@_; return $a*$b*$c*$d*$e*$f; } sub direct { return $_[0]*$_[1]*$_[2]*$_[3]*$_[4]*$_[5]; } cmpthese(-5, { 'shifter' => sub {shifter(1,2,3,4,5,6);}, 'assigner' => sub {assigner(1,2,3,4,5,6);}, 'direct' => sub {direct(1,2,3,4,5,6);}, } );
$ perl Benchmark: running assigner, direct, shifter, each for at least 2 CPU +seconds... assigner: 0 wallclock secs ( 2.06 usr + 0.02 sys = 2.08 CPU) @ 384 +577.33/s (n=800690) direct: 3 wallclock secs ( 2.04 usr + 0.00 sys = 2.04 CPU) @ 629 +222.22/s (n=1285501) shifter: 2 wallclock secs ( 2.09 usr + 0.00 sys = 2.09 CPU) @ 294 +563.31/s (n=616521) Rate shifter assigner direct shifter 294563/s -- -23% -53% assigner 384577/s 31% -- -39% direct 629222/s 114% 64% --
That's with perl 5.6.1... Maybe 5.8.0 optimized shift? But you'd have to keep the old values around and have a "front" entry in the AV, and I don't remember seeing anything about that.

In reply to Re: Re: Optimizing existing Perl code (in practise) by RMGir
in thread Optimizing existing Perl code (in practise) by JaWi

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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (10)
    As of 2019-05-22 22:42 GMT
    Find Nodes?
      Voting Booth?
      Do you enjoy 3D movies?

      Results (140 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!