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

I'm currently working on a routine that will benchmark the time it takes to query and write a report.

What would be the best way to benchmark this code? And it must be something I can call for any while loop in the script.
while (@data = $query->fetchrow) {

Any suggestions are helpful :)

qball~"I have node idea?!"

Replies are listed 'Best First'.
Re: Benchmark Wrapper
by Beatnik (Parson) on Apr 05, 2001 at 00:33 UTC
    Time::HiRes is a good alternative to Benchmark, if you want to time inside the actual code... also check DProf.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Benchmark Wrapper
by qball (Beadle) on Apr 05, 2001 at 00:38 UTC
    Actually, I think Benchmark provides a useful way. However, using this code:
    $td = timediff($t1, $t0); print "the code took:",timestr($td),"\n";
    Returns:

    the code took: 0 wallclock secs ( 0.01 usr +  0.00 sys =  0.01 CPU)

    How would extract just the wallclock secs. And what if the code took several minutes...how would I extract minutes vs. secs?

    Any suggestions?

    qball~"I have node idea?!"
Re: Benchmark Wrapper
by arturo (Vicar) on Apr 05, 2001 at 01:18 UTC

    I just want to register caveats about benchmarking SQL and what they mean; first, if others are using the DB, results can differ for reasons having nothing to do with your code.

    Also, rewriting queries is only one way of speeding things up; pay attention to using indexes, and the various DBMS-specific tricks that are out there.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      In my case, I just want an estimate so whoever's running the report will have a stamp of how long it took. It's not really how fast the code is I'm concerned about, rather a general idea of how long it took to run the report.

      By doing this, it will help with a lot of other issues we currently have in the queue. Any thoughts, but specifically to my previous question Re:Benchmark Wrapper.

      qball~"I have node idea?!"
Re: Benchmark Wrapper
by thabenksta (Pilgrim) on Apr 04, 2001 at 23:56 UTC

    Get the time before the loop, and then the time after the loop, and compare.

    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';
      That was my first concept, but I'd like to somehow put it in a sub routine so I don't have to add so many lines to each report loop. Maybe I could just make the compare code a routine and call it within each report routine. Unless, of course, there's a better way.

      qball~"I have node idea?!"

        You could write a subroutine to put before and after the loop. Before the loop, you could have it return the time, and after the loop feed it that time and return the difference. That's the only way I can think of.

        my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';