Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Anything better than 'times' ?

by gone2015 (Deacon)
on Sep 17, 2008 at 14:31 UTC ( #712008=perlquestion: print w/replies, xml ) Need Help??

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

So, I tried timing some code using 'times'.

Perhaps I should know better, but I'm disappointed by the resolution this gives me. Am I stuck with this, or is there some alternative that the assembled experts can point me towards... ?

I find that CLOCKS_PER_SEC is 1000000l, which looks hopeful, but getconf CLK_TCK gives just 100 -- which matches what I get :-(

POSIX::times() doesn't help. Perhaps this is a kernel issue ?


BTW, the Benchmark module doesn't apply. I'm trying to time the same code over lots of different input data sets, not compare different code over the same input data set.

Replies are listed 'Best First'.
Re: Anything better than 'times' ?
by moritz (Cardinal) on Sep 17, 2008 at 14:36 UTC

      I had tried Time::HiRes::clock(), but that gives the same resolution as times. So I thought that was a dead end.

      However, I went back and read again. There are Time::HiRes::clock_gettime($which) and Time::HiRes::getres($which). Where the $which is the name of a POSIX high resolution timer. I confess I had assumed that this was wall-clock.

      The POSIX timer CLOCK_REALTIME claims a resolution of 1E-9 on my machine. Hurrah :-) However, that is wall-clock. Boo :-(

      The Time::HiRes hints that there may be other timers... so, I went digging in the POSIX documentation. I found CLOCK_PROCESS_CPUTIME_ID, which does what the name suggests, and also claims a resolution of 1E-9 on my machine. Hurrah !

      Conclusion, yes: turns out that Time::HiRes is the answer -- thank you. To know that, however, you need to know that:

      • there is a "clock" called CLOCK_PROCESS_CPUTIME_ID -- provided you have a POSIX compliant system, and _POSIX_CPUTIME is defined (which is discovered using getconf).
      • that you use can import that name from Time::HiRes and use it with Time::HiRes::clock_gettime($which) and Time::HiRes::getres($which) to access the timer.
      • there is also a "clock" called CLOCK_THREAD_CPUTIME_ID, which is available if _POSIX_THREAD_CPUTIME is defined.
      So, for my purposes:
      use Time::HiRes qw(clock_gettime CLOCK_PROCESS_CPUTIME_ID) ; $start = clock_gettime(CLOCK_PROCESS_CPUTIME_ID) ; # replaces $start + = (times)[0] ... $end = clock_gettime(CLOCK_PROCESS_CPUTIME_ID) ; # replaces $end + = (times)[0]
      is the trick -- which I note in case it's of use to anyone like me who knew nothing of it until today.

Re: Anything better than 'times' ?
by grinder (Bishop) on Sep 17, 2008 at 20:39 UTC

    If you're on FreeBSD, you could use BSD::Process to measure elapsed kernel and userland time with microsecond precision.

    • another intruder with the mooring in the heart of the Perl

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-06-27 15:13 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (88 votes). Check out past polls.