Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Code Profiling

posted by pudge on 2001.10.25 10:11   Printer-friendly
jdavidb writes "I've come to a situation where I have a very inefficient search algorithm taking six minutes to accomplish what should be a simple task. I've brought the minds of many coworkers to bear on it, and we've collectively realized that it's difficult to figure out exactly where the performance bottleneck is. I need to optimize something, but what?

Obviously, the solution is to profile the code and find out where the real bottleneck is. But I've never done that before, not even in C. I've heard rumors of DProf and stuff like that, but I don't know if that's the module I should use, or even what my choices are. What options do I have for code profiling, what tutorials, introductions, or other articles exist out there, and what common novice goofs am I about to spend today repeating?"

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • When you have a script that is composed of many subroutines, DProf can tell you how much time each subroutine is taking. Programming Perl, 3rd Edition [] has a chapter by Gnat Torkington [] on this very subject. Before I read that small section, I was a lost about profiling as anyone could be. There's a bit more to it than just using DProf, because that module writes it's findings in a rather cryptic format. Instead, there's a front end, dprofpp, that will help.

    dprofpp -p scriptname

    should do it.

  • brian d foy has written an article entitled Profiling in Perl [] which covers using Devel::SmallProf [], a per-line code profiler. It is quite good for small code snippets. The article walks you through how to use the module.

    For larger bits of code, Devel::DProf [] is very good. The documentation walks you through how to use the module simply.

    Oh, and Devel::Cover [] is starting to do profiling too...

  • There is also a chapter in Martin Brown's "Debugging Perl" on DProf (chapter 12 - Automatic Optimizations"
    I go back to sleep now....
  • Thanks for the help, folks. My 6 minute 40 second worst (?) case search is now virtually instantaneous. Actual problems turned out to be in reading from the disk. File::Tail::read to be specific. Not a problem with the module, just my abuse of it.

    For the record, it doesn't help you to read a whole file in and then try to perform an efficient search. You absolutely have to, have to, have to read only parts of the file with seek/tell/sysread type functions. There's no getting around it! But, you'll b

    J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
    • I've had a similar experience here, I need to pass thru a 200000 line text file 3000+ times.

      I was converting a date+time in ISO format to epoch seconds every time using Time::Local, now I do it up front (an obvious optimisation, didn't even need a profiler) and that *did* improve the speed dramatically, but it was still slow. The solution? Pre-build an index into the file to avoid having to go thru each line (even tho I was calling next if the line wasn't in the timeframe I was looking for) and lo and beh