If you're trying to optimize your program or measure its efficiency, it's very useful to know how much processor time it uses. For that, calendar time and elapsed times are useless because a process may spend time waiting for I/O or for other processes to use the CPU. However, you can get the information with the functions in this section.
CPU time (the section called “Time Basics”) is represented by the data type clock_t, which is a number of clock ticks. It gives the total amount of time a process has actively used a CPU since some arbitrary event. On the GNU system, that event is the creation of the process. While arbitrary in general, the event is always the same event for any particular process, so you can always measure how much time on the CPU a particular computation takes by examinining the process' CPU time before and after the computation. In the GNU system, clock_t is equivalent to long int and CLOCKS_PER_SEC is an integer value. But in other systems, both clock_t and the macro CLOCKS_PER_SEC can be either integer or floating-point types. Casting CPU time values to double, as in the example above, makes sure that operations such as arithmetic and printing work properly and consistently no matter what the underlying representation is.
Note that the clock can wrap around. On a 32bit system with CLOCKS_PER_SEC set to one million this function will return the same value approximately every 72 minutes.
For additional functions to examine a process' use of processor time, and to control it, Chapter 23.
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. the section called “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.
int function>CLOCKS_PER_SEC/function> The value of this macro is the number of clock ticks per second measured by the clock function. POSIX requires that this value be one million independent of the actual resolution.
int function>CLK_TCK/function> This is an obsolete name for CLOCKS_PER_SEC.
function>clock_t/function> This is the type of the value returned by the clock function. Values of type clock_t are numbers of clock ticks.
clock_t function>clock/function> (void) This function returns the calling process' current CPU time. If the CPU time is not available or cannot be represented, clock returns the value (clock_t)(-1).
The times function returns information about a process' consumption of processor time in a struct tms object, in addition to the process' CPU time. the section called “Time Basics”. You should include the header file sys/times.h to use this facility. function>struct tms/function> The tms structure is used to return information about process times. It contains at least the following members:
This is the total processor time the calling process has used in executing the instructions of its program.
This is the processor time the system has used on behalf of the calling process.
This is the sum of the tms_utime values and the tms_cutime values of all terminated child processes of the calling process, whose status has been reported to the parent process by wait or waitpid; see the section called “Process Completion”. In other words, it represents the total processor time used in executing the instructions of all the terminated child processes of the calling process, excluding child processes which have not yet been reported by wait or waitpid.
This is similar to tms_cutime, but represents the total processor time system has used on behalf of all the terminated child processes of the calling process.
All of the times are given in numbers of clock ticks. Unlike CPU time, these are the actual amounts of time; not relative to any event. the section called “Creating a Process”.
clock_t function>times/function> (struct tms *buffer) The times function stores the processor time information for the calling process in buffer.
The return value is the calling process' CPU time (the same value you get from clock(). times returns (clock_t)(-1) to indicate failure.
Portability Note: The clock function described in the section called “CPU Time Inquiry” is specified by the ISO C standard. The times function is a feature of POSIX.1. In the GNU system, the CPU time is defined to be equivalent to the sum of the tms_utime and tms_stime fields returned by times.