The Perl Profiler
Do you want to make your program faster? Well, of course you do. But first you should stop and ask yourself, "Do I really need to spend time making this program faster?" Recreational optimization can be fun,[2] but normally there are better uses for your time. Sometimes you just need to plan ahead and start the program when you're going on a coffee break. (Or use it as an excuse for one.) But if your program absolutely must run faster, you should begin by profiling it. A profiler can tell you which parts of your program take the most time to execute, so you won't waste time optimizing a subroutine that has an insignificant effect on the overall execution time.
Perl comes with a profiler, the Devel::DProf
module. You can use it to profile the Perl program in
mycode.pl by typing:
perl -d:DProf mycode.pl
Even though we've called it a profiler--since that's
what it does--the mechanism DProf employs is the
very same one we discussed earlier in this chapter.
DProf is just a debugger that records the time Perl
entered and left each subroutine.
When your profiled script terminates,
DProf will dump the timing information to a file
called tmon.out. The dprofpp
program that came with Perl knows how to analyze
tmon.out and produce a report. You may also use
dprofpp as a frontend for the whole process with
the -p switch (see described later).
Given this program:
outer(); sub outer { for (my $i=0; $i < 100; $i++) { inner() } } sub inner { my $total = 0; for (my $i=0; $i ...