The Perl Profiler

You can supply an alternative debugger for Perl to run by invoking your script with the -d:module switch. One of the most popular alternative debuggers for Perl is DProf, the Perl profiler. As of this writing, DProf is not included with the standard Perl distribution, but it is expected to be included soon.

Meanwhile, you can fetch the Devel::DProf module from CPAN. Once it has been properly installed on your system, you can use it to profile the Perl program in testpgm by typing:

perl -d:DProf testpgm

As your script runs, DProf gathers profile information. When the script terminates, the profiler dumps the gathered information to a file called tmon.out. A tool such as dprofpp, which is supplied with the Devel::DProf package, can be run to interpret the profile. If you run dprofpp against the tmon.out file created by DProf in the example above, you’ll see something like the following:

% dprofpp tmon.out Total Elapsed Time = 0.15 Seconds User+System Time = 0.1 Seconds Exclusive Times %Time Seconds # Calls sec/call Name 30.0 0.030 1 0.0300 Text::Wrap::CODE(0x175f08) 20.0 0.020 1 0.0200 main::CODE(0xc7880) 20.0 0.020 1 0.0200 main::CODE(0xfe480) 10.0 0.010 1 0.0100 Text::Wrap::CODE(0x17151c) 10.0 0.010 10 0.0010 Text::Tabs::expand 0.00 0.000 1 0.0000 lib::CODE(0xfe5b8) 0.00 0.000 3 0.0000 Exporter::export 0.00 0.000 1 0.0000 Config::FETCH 0.00 0.000 1 0.0000 lib::import 0.00 0.000 1 0.0000 Text::Wrap::CODE(0x171438) 0.00 0.000 3 0.0000 vars::import 0.00 0.000 3 0.0000 ...

Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.