Are There Perl Optimization Guides? 11
ara818 asks: "I have written a 4,000-line personal web assistant using Perl. After getting everything to work I am now working on making the code run faster. The problem I keep running into though is that there are so many ways to do the same thing in Perl that I don't know which is faster. Right now, I am working on intuition but I'd really like a site or book that could give me at least a few pointers or some guidelines. Is there any such resource available for Perl, or for that matter, other popular programming languages?"
The mod_perl guide (Score:2)
http://perl.apache.org/guide/performa nce.html [apache.org]
The Camel Book (Score:1)
Good Book (Score:1)
What's the status of the Perl-to-C Compiler? (Score:1)
memoize! (Score:1)
Also, be sure to avoid overloading the garbage collector. I know that a lot of Java (another garbage collected language) programs needlessly allocate String objects by using the concatenation operator. Find out a little more about how Perl's GC works and you'll probably see that your code is working it a bit too hard at least someplace.
~wog
Do you really need it? (Score:1)
Time Efficiency (Score:4)
my %keywords;
for (@keywords) {
++$keywords{$_};
}
Then test $keywords{$_} for a nonzero value to see if $_ is a keyword.
"foundstring" =~
while () {
print if
}
print if
is likely to be faster than:
print if
at least for certain values of one-hump and two. This is because the optimizer likes to hoist ceertain simple matching operations up into higher parts of the syntax tree and do very fast matching with a Boyer-Moore algorithm. Complicated patterns defeat this.
while () {
next if
next if
chop;
@line = split(/,/);
opendir(DIR, ".");
@files = sort grep(!/^\./, readdir(DIR));
closedir(DIR);
if ($a) { $foo = $a; }
elsif ($b) { $foo = $b; }
elsif ($c) { $foo = $c; }
Two few quick things (Score:2)
Do not shift and do not increment integers for cycles unless necessary (perl integer math sucks and shift loads the garbage collector). Foreach is faster.
Do not compare and substr, regexp is faster. If anything can be formulated as a regexp, regexp it. That what perl is good at. Have fun.
Zen of Code Optimization -- Abrash (Score:1)
Similar experience (Score:1)
A good debug function is the place to start (Score:1)
Sprinkle liberally through the code.
Study the results. You'll find that often what you though was slow isn't and what you thought was fast is slow. In one case a CGI I saw was taking 2 seconds to just load all the libs that were being requird even though most were not needed!
In another case we found that the fastest way to beuild a free text endgine was to munge the input into a regex and use the builtin grep on the data (cut the search from 500ms to sub 100ms of CPU).
Overall it's very application dependent - most of the optimizations (apart from the regex stuff) are about good coding practice as much as anyting else.