Devel::DProf
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,[177] 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.plEven 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 (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 < 1000; $i++) { $total ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access