Perl is an incredibly flexible language, but its ease of use can lead to some sloppy and lazy programming habits. We’re all guilty of them, but there are some quick steps you can take to improve the performance of your Perl applications. This article looks at the key areas of optimization, which solutions work and which don’t, and how to continue to build and extend your applications with optimization and speed in mind.
This is superb. IBM developerWorks is full of gems like this. Let there be more of these articles!
See the critique written by others at Perl Monks: http://perlmonks.org/?node_id=401398
A little critique from another Perl user:
– Concatenating Strings:
Why would someone use arrays to concatenate strings? It’s unnatural. The example given is rather contrived and doesn’t really reflect real life code, hence it’s not useful.
– String Handling:
When you run a Perl program, the program goes through a compile phase and then a run phase. During the compile phase, literal strings that do not contain interpolated variables and that are quoted in either single or double quotes will be compiled to the same byte code, and hence will execute at the same speeds. The quotes do not matter if there is no interpolation.
What the author failed to mention is that print() is slow, and its calls should be minimized (if speed is of concern). So, if you have to print a lot of text, it is much faster to assemble a large string of what you want to print, and then call the print() function once.
– Loops:
Using map() in void context is frowned upon. In older Perls, map() would actually create a list, which would then be thrown away. Recent Perls detect if map() is called in void context, and don’t generate the list if it is the case. In any case, using a for loop is better, IMHO:
$values->{$_}->{result} = $values->{$_}->{adda}+$values->{$_}->{addb} for keys %{$values};
– Sorts:
The Schwartzian transform would be perfect for the job.
– Use Autoloader:
Excellent idea. The problem is that you save a few seconds in compile time, but waste (perhaps more?) time in runtime because for any function that is not defined, Perl has to first figure out that is not defined, and then call AutoLoad which will figure out the correct function. A better solution would be to only load the functions you need using:
use MyModule qw/Sub1 Sub2/;
All in all, a good article. Thanks.
– Sorts:
The Schwartzian transform would be perfect for the job.
Use the Schwartz, Luke
I stopped reading when the article started explaining that…
$concat = 999999 x ‘abcdefghijklmnopqrstuvwxyz’;
would produce 999999 copies of the alphabet. Except it won’t since the ‘x’ operator is not commutative. What they really meant was…
$concat = ‘abcdefghijklmnopqrstuvwxyz’ x 999999;
This article has obvious mistakes you would expect from someone who wrote a book about perl.
He could have chosen better/easier-to-read examples.
Most of all, his code seems to be suffering from premature optimization (a.k.a. the root of all evil).
The authour says to write perl code using the optimization techniques, then profile, rinse, repeat.
Maybe getting something working first, then profiling to find the bottlenecks since Pareto’s Principle (80/20 rule) will save you from optimzing the 80% of the code that doesn’t matter as much?