(e-mail address removed) (buildmorelines) wrote in
Are there any books or guide to Perl function and operator and syntax
(not algorithem) optimzation? like a guide that says what function or
way of doing something is faster than another.
Two of them I discovered were scalar(@array) is faster than $#array,
Hmmm ...
D:\Home> cat t.pl
#! perl
use strict;
use warnings;
my @arr = ( 1 );
print 'scalar @arr = ', scalar(@arr), "\n";
print '$#arr = ', $#arr, "\n";
__END__
D:\Home> perl t.pl
scalar @arr = 1
$#arr = 0
Something about apples and oranges comes to mind.
and pop(@array) is faster than $array[-1].
#! perl
use strict;
use warnings;
{
my @arr = ( 1 );
print "The last element of \@arr is: $arr[-1]\n";
print "And I am number $arr[-1]\n";
}
# compare with
{
my @arr = ( 1 );
print "The last element of \@arr is: @{[ pop @arr ]}\n";
print "And I am number $arr[-1]\n"; # ooops
}
__END__
D:\Home> perl t.pl
The last element of @arr is: 1
And I am number 1
The last element of @arr is: 1
Use of uninitialized value in concatenation (.) or string at t.pl line 17.
And I am number
Did I mention apples and oranges before?
Are there any more of those kinds of optimzations?
These are not optimizations.
The fasterness
Cute.
was determined by running a perl script that runs a loop 20x that runs
another perl through `` and processes smallprof.out getting the average.
The other perl being run through `` is a one line perl program that is
like "$r = pop(@array);" or "$r = $array[-1];" running with -d:SmallProf
on the command line arguments to the interpreter.
I am not sure exactly what you are doing, but, hmmmmm ...
D:\Home> cat t.pl
#! perl
use strict;
use warnings;
use Benchmark qw(cmpthese);
cmpthese( 1_000_000, {
apples => sub {
my @arr = ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 );
my $r = $arr[-1];
++$r;
},
oranges => sub {
my @arr = ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 );
my $r = pop @arr;
++$r;
}
}
);
__END__
On Windows 98 PIII 500, 128 MB, Winamp playing ...
D:\Home> perl -v
This is perl, v5.8.4 built for MSWin32-x86-multi-thread
D:\Home> perl t.pl
Rate oranges apples
oranges 94340/s -- 0%
apples 94340/s 0% --
Now, to make these apples and oranges comparable, we can insert the
following line in the oranges sub:
push @arr, $r;
after we initialize $r.
D:\Home> perl t.pl
Rate oranges apples
oranges 82372/s -- -11%
apples 92937/s 13% --
Now, FreeBSD 5.2.1-RELEASE, AMD Athlon 2600, 256 MB, Apache, PostgreSQL,
and a whole bunch of other stuff running ...
asu1@recex:~ > perl -v
This is perl, v5.8.5 built for i386-freebsd-64int
asu1@recex:~ > perl t.pl
Rate apples oranges
apples 250000/s -- -1%
oranges 251473/s 1% --
With the
push @arr, $r;
I get:
asu1@recex:~ > perl t.pl
Rate oranges apples
oranges 208469/s -- -17%
apples 250980/s 20% --
The point of this exercise is not to prove you wrong or anything ... It is
just to show you that comparing apples and oranges does not make sense:
Choose between pop @arr and $arr[-1] based on whether you need/want to
preserve the array, not based on speed.
Sinan.