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):
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.
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.
You can look at the performance of individual lines, blocks (loops, etc.) or overall.
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.
Increase ad revenue 50-250% with Ezoic
More Articles by Anthony Lawrence
Find me on Google+
© 2011-07-16 Anthony Lawrence