Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: How to measure time for a particular code section?

by abdullah.yildiz (Novice)
on Nov 24, 2012 at 21:27 UTC ( #1005427=note: print w/replies, xml ) Need Help??

in reply to Re: How to measure time for a particular code section?
in thread How to measure time for a particular code section?

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.

Replies are listed 'Best First'.
Re^3: How to measure time for a particular code section?
by zentara (Archbishop) on Nov 25, 2012 at 11:51 UTC
    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
      Thank you for your suggestions.
Re^3: How to measure time for a particular code section?
by Anonymous Monk on Nov 24, 2012 at 21:51 UTC

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

      Thank you for your answer. I meant I waited too much to finish it. Additionally, The code
      my $start_time = [Time::HiRes::gettimeofday()]; . . . my ($user, $system, $child_user, $child_system) = times; print "wall clock time was ", Time::HiRes::tv_interval($start_time), " +\n", "user time for $$ was $user\n", "system time for $$ was $system\n", "user time for all children was $child_user\n", "system time for all children was $child_system\n";
      gives different results with respect to benchmark. Sorry for any foolish comments.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005427]
[1nickt]: I have been an apostate in the Apple orchard for almost 15 years. But Dells are still high quality it seems, unlike Macs these days.
[1nickt]: And you can still configure the system at will, on the "Small Business" site.
[Corion]: 1nickt: I bought a 2015 XPS 13, which is a very nice and shiney (and somewhat overpriced) machine, but I think it's still better priced than an Apple machine. Now it just has to hold up 10 years like the old one did ;)
[1nickt]: Ah, you got Linus' fave? With Ubuntu? 4k display?
[1nickt]: ( The 17" monster I just bought is for my wife, who doesn;t leave the house with it...)

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2017-03-27 13:04 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.