APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Perl Profiling with Devel::NYTProf

© July 2011 Anthony Lawrence

I don't think I have ever used a profiler on my own code. The reasons are simple: I don't write much that is very complicated, so any bottlenecks are usually rather easy to spot. Most of what I do is ad hoc and limited use anyway, so speed is seldom a consideration.

However, earlier this week I briefly considered getting involved in a large project that involved an interesting mix of shell scripts, Perl, C and ancient Filepro. The work would have been to help improve performance across all those varying scripts and programs. It sounded very interesting.

I started looking around for Perl profilers and I came across Devel::NYTProf. There are surely other similar tools, but my brief look at this convinced me that if I were going to be doing Perl performance hacking, this would be something good to have.

A test spin of NYTProf

I ran this against a short script I use here at this site. The script doesn't need any speed improvement, so this is just to showcase the features.

After installing NYTProf with CPAN, I ran "perl -d:NYTProf" followed by my program name and its arguments. I then ran "nytprofhtml --open" and was presented with this screen (click to enlarge):

NYTProf main screen - click to enlarge

By the way, this is all fairly complicated stuff. I had some difficulty just finding the documentation and I don't know what all of this means. The "Calls" column is obvious. The docs explain the times columns:

Overall subroutine times are reported with a headline like spent 10s (2+8) within .... In this example, 10 seconds were spent inside the subroutine (the "inclusive time") and, of that, 8 seconds were spent in subroutines called by this one. That leaves 2 seconds as the time spent in the subroutine code itself (the "exclusive time", sometimes also called the "self time").

I don't know what the "P" and "F" columns are, though.

We obviously have value here: right away we know which subroutines see heavy use. The first is "sleep", and as that is deliberate and needed in this script, I can ignore that. The "system" calls are unavoidable also, as are the "readline". I do tend to overuse Perl "match" (=~ and the like), often doing that when equality would be perfectly fine, so if I were looking for performance boosts, I might drill into that, but let's ignore those, skip right by BEGIN ( not much I can do about that) and look at "shuffle".

"shuffle" is a routine that randomizes an array. I didn't write it; I cribbed it from somewhere, and I don't think I could improve it, but it helps show off NYTProf's features, so we'll use that.

NYTProf subroutine detail screen - click to enlarge

This has drilled into the actual subroutine. Notice that the commentary shows what called this. That's important, because different arguments affect speed. NYTprof lets you find the performance for each invocation.

NYTProf subroutine called from - click to enlarge

NYTProf subroutine called from - click to enlarge

You can look at the performance of individual lines, blocks (loops, etc.) or overall.

NYTProf subroutine called from - click to enlarge

NYTProf subroutine called from - click to enlarge

This is all so much fun that I almost wish I needed to improve this script!

There'd be plenty of opportunity at that project, right? Well, no. I ultimately turned down the work. As I started looking at a relatively simple shell script that needed some enhancements, I realized that I'm just too old and too tired for that kind of work. I was finding the shell script hard to understand and I realized that there was worse to come, so I bowed out, with regret - regret that I couldn't help someone who really needed it and regret for the lost potential income. Reality is what reality is, though: I am not able to take on something like that at this point in my life.

Still, I got to see what NYTProf has to offer and to share it with you, so that has value.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Finding Perl performance bottlenecks with Devel::NYTProf

Inexpensive and informative Apple related e-books:

iOS 8: A Take Control Crash Course

Sierra: A Take Control Crash Course

Take Control of Numbers

Take Control of Parallels Desktop 12

Take Control of iCloud, Fifth Edition

More Articles by © Anthony Lawrence

Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

There are two ways to write error-free programs; only the third one works. (Alan Perlis)

Linux posts

Troubleshooting posts

This post tagged:




Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode