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
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 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.
The value of this macro is the number of clock ticks per second measured
clock function. POSIX requires that this value be one
million independent of the actual resolution.