Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Trouble with Benchmark

by punch_card_don (Curate)
on Oct 05, 2004 at 22:31 UTC ( [id://396803]=perlquestion: print w/replies, xml ) Need Help??

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

Could someone please tell me what's wrong with this use of Benchmark? All I get from it is zero time measured.
use Benchmark; $start = new Benchmark; ... bunch of code ... $end = new Benchmark; $diff = timediff($end, $start); print "<p>Time taken was ", timestr($diff, 'all'), " seconds";
Thanks.

Replies are listed 'Best First'.
Re: Trouble with Benchmark
by dave_the_m (Monsignor) on Oct 05, 2004 at 23:32 UTC
    Works for me. Perhaps your code doesn't take long to run??
    use Benchmark; $start = new Benchmark; 1 for 1..10000000; $end = new Benchmark; $diff = timediff($end, $start); print "<p>Time taken was ", timestr($diff, 'all'), " seconds\n"; __END__ $ perl585 /tmp/p2 <p>Time taken was 4 wallclock secs ( 4.28 usr 0.01 sys + 0.00 cusr + 0.00 csys = 4.29 CPU) seconds $

    Dave.

Re: Trouble with Benchmark
by punch_card_don (Curate) on Oct 06, 2004 at 00:08 UTC
    Holy cow! You're right. I stuck in a long loop such as yours and suddenly there are a few tenths of a second showing.

    I'm astounded because the code I was running was a query of a mysql database with over 12-million rows, pulling out 4,000 rows. In theory, that means the query took less than the 1/100 of a second mimimum displayable? Impressive.

    Anyway, thanks.

      4,000 rows isn't much for most databases. If you've run the query before, it's likely the database still has the needed pages in cache - it might even have the query, its plan, and the result in cache. And if your database is running on the same machine as the benchmark, results are expected to be fast. Drop your indices, shutdown your database, restart the database, and run your program again. That might give you something to measure.
        Well of course nothing's as simple as it should be.

        OK - what's this? Shut down the database? I don't recall starting it up. I connected to it - is that what you mean? But I disconnect each time the script terminates.

        The benchmark on the same machine as the database? You mean the module?

        Drop my indices? The whole point is to measure performance with an index if that's how it's going to run, no?

Re: Trouble with Benchmark
by punch_card_don (Curate) on Oct 06, 2004 at 13:52 UTC
    So I've started dabbling with the "timethis()" function, figuring better to measure 1,000 iterations of the actual code than to insert a time-waster just to make some time show on the clock.

    But the results are odd:

    $sql = "SELECT COUNT(*) FROM theTable WHERE column = $value"; $iterations = 1000; $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->err +str); $exec_time = timethis($iterations, ' $sth->execute() or die("Could not execute!" . $dbh->errstr); $count = $sth->fetchrow_array( ); ');
    produces

    timethis 1000: 26 wallclock secs ( 0.03 usr + 0.02 sys = 0.05 CPU) @ 20000.00/s (n=1000) (warning: too few iterations for a reliable count)

    26 wallclock seconds but only 0.05 CPU seconds? And if I try to go to 5,000 iterations, the server returns an error.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://396803]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-16 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found