Profiling Perl

As we write this, Perl comes with a profiler called Devel::DProf. However, by the time you read this, it might be gone. Perl v5.16, which is scheduled for release around the same time this book hits the shelves, removes this old profiler. Most people using a profiler have moved on to another profiler, Devel::NYTProf. We’ll tell you about Devel::DProf since it’s still in Perl, but we’ll also tell you about the new one, which doesn’t come with Perl.

These profilers are not lightweight, and they aren’t your only options for a profiler. CPAN also holds Devel::SmallProf, which reports the time spent in each line of your program. That can help you figure out if you’re using some particular Perl construct that is being surprisingly expensive. Most of the built-in functions are pretty efficient, but it’s easy to accidentally write a regular expression whose overhead increases exponentially with the size of the input. See also the section Efficiency in Chapter 21 for other helpful hints.

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

Get Programming Perl, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.