perl optimazation guide/book

B

buildmorelines

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,
and pop(@array) is faster than $array[-1]. Are there any more of those
kinds of optimzations?

The fasterness 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.
 
A

A. Sinan Unur

(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.
 
J

John Bokma

buildmorelines said:
The fasterness 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.

http://search.cpan.org/~nwclark/perl-5.8.5/lib/Benchmark.pm

Note that benchmarks also depend on which Perl version you are running.
 
T

Tad McClellan

buildmorelines said:
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,


They do not do the same thing.

and pop(@array) is faster than $array[-1].


They do not do the same thing either.

Are there any more of those
kinds of optimzations?


You mean the kind that don't do the same thing? :)

The fasterness was determined by running a perl script


You can use the Benchmark module for benchmarking, but when you do,
you should probably benchmark code that does the same thing.
 
B

buildmorelines

John Bokma said:
buildmorelines said:
The fasterness 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.

http://search.cpan.org/~nwclark/perl-5.8.5/lib/Benchmark.pm

Note that benchmarks also depend on which Perl version you are running.

Here you go.

This is perl, v5.8.4 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)

Copyright 1987-2004, Larry Wall

Binary build 810 provided by ActiveState Corp. http://www.ActiveState.com
ActiveState is a division of Sophos.
Built Jun 1 2004 11:52:21
 
C

ctcgag

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.

I rather suspect that such things exists, but I rather doubt they are
worthwhile.
Two of them I discovered were scalar(@array) is faster than $#array,
and pop(@array) is faster than $array[-1]. Are there any more of those
kinds of optimzations?

Well, in neither case do the alternatives you present do the same thing.
If you don't care whether you get the right answer, you can get
extreme optimization by replacing all of your code with:

print rand();
exit;


Xho
 
L

Laura

buildmorelines said:
John Bokma said:
buildmorelines said:
The fasterness 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.

http://search.cpan.org/~nwclark/perl-5.8.5/lib/Benchmark.pm

Note that benchmarks also depend on which Perl version you are running.

Here you go.

This is perl, v5.8.4 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)

Copyright 1987-2004, Larry Wall

Binary build 810 provided by ActiveState Corp. http://www.ActiveState.com
ActiveState is a division of Sophos.
Built Jun 1 2004 11:52:21

For relatively simple algorithms, the best way to test speed and
optimizations is to write an actual program in C which is line-for-line
equivalent to your Perl script. C, while primitive, is considered to be
the gold standard in speed. Some institutions will rate their code as 2x,
4x, 8x etc... as a multiple of the speed at which the code would run if it
were in C. Perl will do this conversion for you if you use the appropriate
module.
 
U

Uri Guttman

L> For relatively simple algorithms, the best way to test speed and
L> optimizations is to write an actual program in C which is
L> line-for-line equivalent to your Perl script. C, while primitive,
L> is considered to be the gold standard in speed. Some institutions
L> will rate their code as 2x, 4x, 8x etc... as a multiple of the
L> speed at which the code would run if it were in C. Perl will do
L> this conversion for you if you use the appropriate module.

again you have said a bunch of balderdash. you obviously don't know c
nor perl well enough because you say stuff like "do a line-for-line"
translation. that never gives an optimal translation from any language
to any other. and did you know some perl code will run faster than
'equivilent' c code? and perl's 'conversion to c' module is never
recommended and never used in the real world?

i am waiting for your next massively wrong missive.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,795
Messages
2,569,644
Members
45,359
Latest member
1854578

Latest Threads

Top