Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

How to measure time for a particular code section?

by abdullah.yildiz (Novice)
on Nov 24, 2012 at 20:50 UTC ( #1005424=perlquestion: print w/replies, xml ) Need Help??
abdullah.yildiz has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I use
#START $start_time = [Time::HiRes::gettimeofday()]; #MY CODE GOES HERE #FINISH $diff = Time::HiRes::tv_interval($start_time);
Does $diff give the real execution time during which the particular code is executed?

Replies are listed 'Best First'.
Re: How to measure time for a particular code section?
by zentara (Archbishop) on Nov 24, 2012 at 21:02 UTC
      Thank you for your answer.
      Here is my problem: I want to sort 10 million random integers:
      $start = Benchmark->new(); #RUN THE ALGORITHM @sorted_numbers = sort { $a <=> $b } @numbers; $end = Benchmark->new(); $diff = timediff( $end, $start );
      And then it returns: 18 wallclock secs (14.59 usr + 0.97 sys = 15.55 CPU) However it takes much more than that. Could you help me about this? I'm a bit confused. Thank you.
        I'm a bit confused

        Using time to measure code execution is not going to be accurate everytime it is run. What if the load on the cpu is heavier during one test, say for example because a cron job is running, or whatever else is eating up cpu cycles. The only way to really measure code execution, is how many cpu cycles it takes to get the task done, the time taken is a function of many things going on in the computer, from random delays in hard drive access, other processes suddenly using up cpu cycles, and just plain randomness. The way to get a good benchmark, would be to run the test code a thousand times over the course of a day, and take the average time.

        See Lies, Damn Lies and Benchmarks and No More Meaningless Benchmarks! for example. You can google for more discussions on the deceptive nature of benchmarks yourself.

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

        "..However it takes much more than that.."
        How do you know that?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005424]
Approved by Old_Gray_Bear
[choroba]: Shouldn't you replace it with your app's name?
[james28909]: i figured it out
[james28909]: upon trying various solutions, i was forgetting to package up the script into an executable
[marto]: that'll do it
[james28909]: im glad im not in charge of anything important, like filling up peanut butter jars

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2017-07-28 14:19 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (431 votes). Check out past polls.