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);},
}
);
Results:
$ perl testSubs.pl
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.
--
Mike
-
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.