mcogan1966 has asked for the wisdom of the Perl Monks concerning the following question:

For a project I am currently working on, the client would like some kind of "proof" of efficiency for documentation purposes. I would like to be able to show the "time" it takes for the processes to run. It was suggested to me that Perl has a nanosecond timer function that could be of use.

Does anyone out there have some experience with this? Or better, does anyone know of a different good way to do this? As far as I can tell, the code itself is fairly fast as-is, with little room for improvment. But $client seems to want some documentation of proof.

Replies are listed 'Best First'.
Re: Perl code timing
by Ovid (Cardinal) on Oct 31, 2003 at 18:39 UTC

    I'm not sure what your client is looking for. Does the client want to know if your code is faster than something else? If the code is fast enough, than it's fast enough. (Sometimes phrasing something that simple is what it takes to convince people).

    Other options:

    • Devel::Dprof
    • Devel::Profile (I like this module. Easy to use and it doesn't segfault)
    • Devel::Profiler (A replacement for Devel::Dprof)
    • Ask the client what number he or she wants, write it down on a piece of paper and hand it back.

    Cheers,
    Ovid

    New address of my CGI Course.

      Take a look at the Benchmark module

        Feh. Lately I've been doing so much work with profiling that I forgot an obvious answer :)

        Cheers,
        Ovid

        New address of my CGI Course.

      Nothing all that complicated really.
      It's more bureauocratic ca-ca than anything else.
      But they would like to see that I ran some kind of 'metric' and kluged some data together to show that the code itself is reasonably well written and efficient.
      I can make the data do what I want it to do, I'm just looking for some way to get some data that I can use in this case.

      There will be a comparison to a previous 'version' of what I'm now writing. But that version was written by $someone_else, and therefore not much my concern. What I've written runs a touch slower, but provides significanly better results. And it's the better results that needs the bigger play. I just need to show some kind of numbers on the relative speed of both programs. If need be, I can outright 'fudge' numbers with a stopwatch. I'm just looking for something technical enough that I can make a manger's eyes glaze over. :)
Re: Perl code timing
by sauoq (Abbot) on Oct 31, 2003 at 18:49 UTC
    It was suggested to me that Perl has a nanosecond timer function that could be of use.

    Whoever suggested such was probably referring to Time::HiRes. The granularity you actually get is, of course, system defined.

    In addition to the Devel:: modules Ovid mentioned, the Benchmark module might be helpful. Maybe.

    The other advice Ovid gave you was right on. Find out what the client wants. What would constitute '"proof" of efficiency' in client's mind? Try to nail that down right away; otherwise, you may be in for some headaches.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Perl code timing
by ChrisR (Hermit) on Oct 31, 2003 at 18:44 UTC
    I have used this one time and it seemed to work pretty well. Here's how I did it (but I'm sure there are many other/better ways)
    use strict; use Time::HiRes qw ( gettimeofday ); ...[some code] my $start = 0; ...[some code] print "Starting process 1... "; $start = Time::HiRes::time(); ...[process code] print "(" . (Time::HiRes::time() - $start) . " seconds)\n"; print "Starting process 2... "; $start = Time::HiRes::time(); ...[process code] print "(" . (Time::HiRes::time() - $start) . " seconds)\n";
Re: Perl code timing
by jeffa (Bishop) on Oct 31, 2003 at 19:35 UTC
    I am currently working with a mod_perl handler written by Maverick. Inside his "engine", he pushes calls to Time::HiRes::time inside an array. After all processing is down, he reports that array in a format like so:
    Total Time: 0.143684983253479
    ----------------------------------------
    0.00020  0.14% template dir resolution
    0.01821 12.67% session attachment
    0.00014  0.10% parameter parsing
    0.00025  0.18% config section resolution
    0.03901 27.15% handler for user/credit view
    0.00010  0.07% result packing
    0.00715  4.98% handler for skeleton handle
    0.00012  0.08% result packing
    0.00775  5.39% handler for status_widget handle
    0.00004  0.03% result packing
    0.05102 35.51% template open
    0.00050  0.35% param packing
    0.01122  7.81% skeleton open
    0.00797  5.55% end
    
    I haven't seen the code that generates the report, but basically all you do is find the delta times between each element in the array. Once you have those numbers and the total time, you can compute the percentage.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Perl code timing
by BrowserUk (Patriarch) on Oct 31, 2003 at 21:50 UTC

    In addition to all the other good advice, you might want to take a peek at Benchmark::Timer. It's a constituant part of the Benchmark module.

    It has the nice ability to allow you to start and stop as many timers as you want in whatever order, and more than once. It accumlates the times for each named timer and provides a nicely formatted 'potted' report on demand.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!
    Wanted!

Re: Perl code timing
by etcshadow (Priest) on Oct 31, 2003 at 18:46 UTC
    There's also (if you need to roll your own) the Time::HiRes module. Oh, and you could just look into the unix system command time.

    ------------
    :Wq
    Not an editor command: Wq
Re: Perl code timing
by strat (Canon) on Nov 01, 2003 at 09:55 UTC
    I like Benchmark for such problems, e.g.
    use Benchmark; # at the beginning: my $benchmarkStart = Benchmark->new(); # code to benchmark # at the end: my $benchmarkEnd = Benchmark->new(); my $diff = Benchmark::timediff($benchmarkEnd, $benchmarkStart); my $str = Benchmark::timestr($diff); if ($str =~ /(\d+)\s*wallclock secs \(\s*?(\d*?\.\d*?)\s*usr\s*\+\s*(\ +d*?\.\d*?)\s/i) { printf ("Execution time: ~ %.0f seconds\nCPU-Time : %.2f\n", +$1, $2); }

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"