[uClibc] clock() for long-running process

Manuel Novoa III mjn3 at codepoet.org
Tue May 11 12:40:34 UTC 2004


On Tue, May 11, 2004 at 09:55:49AM +0200, Joakim Tjernlund wrote:
> > > hmm, times() returns a clock_t type which is signed. How
> > > should one use times() to calc. elapsed time?
> > > Just type cast its return value to an unsigned long?
> >
> > When I wrote that times() should be used, I meant as it is used by
> > clock() but without the scaling.  clock() measures cpu time used by
> > the process and calculates that by adding the tms_utime and tms_stime
> > fields of the struct tms set by times().  Best done with a custom
> > wrapper function that doesn't do the CLOCKS_PER_SECOND scaling that
> > clock() has to do.
> 
> I see, but using CPU time for measuring elapsed time is not very accurate, is it?

We're talking about usage of clock() like

  start = clock();
  < do some work >
  elapsed = clock() - start;

where the programmer _is_ attempting to measure cpu time.

> I can only find two way to measure elapsed time in Linux, the return
> value from times()(needs scaling) or gettimeofday(). gettimeofday() has
> the disadvantage that it may change time between polls.

If you're trying to measure elapsed wall clock time on linux and you need
only second accuracy, you can use the difference in system uptime.

  #include <sys/sysinfo.h>

  /* Return system uptime in seconds. */

  long uptime(void)
  {
    struct sysinfo info;

    sysinfo(&info);

    return info.uptime;
  }

Manuel



More information about the uClibc mailing list