To get a process' CPU time, you can use the clock
function. This
facility is declared in the header file `time.h'.
In typical usage, you call the clock
function at the beginning
and end of the interval you want to time, subtract the values, and then
divide by CLOCKS_PER_SEC
(the number of clock ticks per second)
to get processor time, like this:
#include <time.h> clock_t start, end; double cpu_time_used; start = clock(); ... /* Do the work. */ end = clock(); cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
Do not use a single CPU time as an amount of time; it doesn't work that way. Either do a subtraction as shown above or query processor time directly. See section Processor Time Inquiry.
Different computers and operating systems vary wildly in how they keep track of CPU time. It's common for the internal processor clock to have a resolution somewhere between a hundredth and millionth of a second.
clock
function. POSIX requires that this value be one
million independent of the actual resolution.
CLOCKS_PER_SEC
.
clock
function.
Values of type clock_t
are numbers of clock ticks.
clock
returns the
value (clock_t)(-1)
.
Go to the first, previous, next, last section, table of contents.