Prev: Process Communication
Next: TCP/IP stream socket
From: Geoff Clare on 15 Apr 2010 08:56 Eric Sosman wrote: >> what is the correct use of times() function? > > clock_t t0, t1; > t0 = times(NULL); > ... do something ... > t1 = times(NULL); > printf ("Used %g CPU seconds\n", > (double)( (t1 - t0) / (CLOCKS_PER_SEC + 0.0) )); > printf ("... or more, if \"something\" took a long time.\n"); > > Used this way, times() is just a substitute for clock(). No. The return value of times() is a measurement of real time, not CPU time; for clock() it is CPU time. Also, passing a NULL argument is not portable. (POSIX requires a pointer to an actual struct tms object to be passed.) (And CLOCKS_PER_SEC should be sysconf(_SC_CLK_TCK) as per my previous post.) > With > a non-NULL argument, you can get additional information that > clock() does not provide. The (non-NULL) struct tms is where you get CPU time from times(), not its return value. -- Geoff Clare <netnews(a)gclare.org.uk>
From: Geoff Clare on 15 Apr 2010 08:44 Eric Sosman wrote: > On 4/14/2010 12:10 PM, sinbad wrote: >> >> how does times() function calculate the processor time. > >> what is the unit for processor time. > > The clock_t type, expressed in (1/CLOCKS_PER_SEC)-second > units. No. You are confusing times() and clock(). It is clock() that uses CLOCKS_PER_SEC; times() uses sysconf(_SC_CLK_TCK) instead. On UNIX systems CLOCKS_PER_SEC is always 1 million, but sysconf(_SC_CLK_TCK) is typically something like 100. -- Geoff Clare <netnews(a)gclare.org.uk>
From: Eric Sosman on 15 Apr 2010 09:17
On 4/15/2010 8:56 AM, Geoff Clare wrote: > Eric Sosman wrote: > >>> what is the correct use of times() function? >> >> clock_t t0, t1; >> t0 = times(NULL); >> ... do something ... >> t1 = times(NULL); >> printf ("Used %g CPU seconds\n", >> (double)( (t1 - t0) / (CLOCKS_PER_SEC + 0.0) )); >> printf ("... or more, if \"something\" took a long time.\n"); >> >> Used this way, times() is just a substitute for clock(). > > No. The return value of times() is a measurement of real time, not > CPU time; for clock() it is CPU time. Thanks for the correction. (I got mixed up by the fact that it returns a clock_t, not a time_t.) -- Eric Sosman esosman(a)ieee-dot-org.invalid |