Format
#include <time.h> clock_t clock(void);
Language Level: ANSI, XPG4
clock returns an approximation of the processor time used by the
program since the beginning of an implementation-defined
time-period that is related to the program invocation. To obtain
the time in seconds, divide the value returned by clock by the
value of the macro CLOCKS_PER_SEC.
Return Value
If the value of the processor time is not available or
cannot be represented, clock returns the value (clock_t)-1.
To measure the time spent in a program, call clock at the start of the program, and subtract its return value from the value returned by subsequent calls to clock.
You cannot always rely on the clock funct ion because calls to the system function may reset the clock.
Example
This example prints the time elapsed since the program
was invoked.
#include <time.h> #include <stdio.h>
double time1,time2,timedif; /* use doubles to show small values */ int i;
int main(void)
{
time1 = (double)clock(); /* get initial time in seconds */
time1 = time1/CLOCKS_PER_SEC;
/* use some CPU time */
for (i = 0; i < 5000000; i++) {
int j;
j = i;
}
time2 = (double)clock(); /* call clock a second time */
time2 = time2/CLOCKS_PER_SEC;
timedif = time2-time1;
printf("The elapsed time is %f seconds\n", timedif);
return 0;
/*************************************************************************
The output should be similar to:
The elapsed time is 0.969000 seconds *************************************************************************/ }
![]()
difftime -- Compute Time Difference
time -- Determine Current Time
<time.h>