C library functions -clock()
Description
C Library Functionsclock_t clock(void)Returns the processor time used since the beginning of program execution (generally the start of the program). To obtain the number of seconds used by the CPU, you need to divide by CLOCKS_PER_SEC.
On 32-bit systems, CLOCKS_PER_SEC equals 1000000, and this function will return the same value approximately every 72 minutes.
Declaration
The following is the declaration of the clock() function.
clock_t clock(void)
Parameter
- NA
Return Value
This function returns the processor time used since the program started. If it fails, it returns a value of -1.
Example
The following example demonstrates the usage of the clock() function.
Example
#include <time.h>
#include <stdio.h>
int main()
{
clock_t start_t, end_t;
double total_t;
int i;
start_t = clock();
printf("Program started, start_t = %ld\n", start_t);
printf("Starting a big loop, start_t = %ld\n", start_t);
for(i=0; i< 10000000; i++)
{
}
end_t = clock();
printf("Big loop finished, end_t = %ld\n", end_t);
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("Total CPU time taken: %f\n", total_t );
printf("Exiting program...\n");
return(0);
}
Let us compile and run the above program, which will produce the following results:
程序启动,start_t = 2614 开始一个大循环,start_t = 2614 大循环结束,end_t = 28021 CPU 占用的总时间:0.025407 程序退出...other extensions