C library functions - clock ()
description
C library functionsclock_t clock (void) Returns the program execution from the (usually at the beginning of the program), the time used by the processor clock.In order to get the number of seconds used by the CPU, you need to divide CLOCKS_PER_SEC.
In 32-bit systems, CLOCKS_PER_SEC equal to 1,000,000, approximately every 72 minutes the function will return the same value.
statement
Here is () statement clock function.
clock_t clock(void)
parameter
- NA
return value
This function returns since the program starts from the time the processor clock is used. If it fails, it returns a value of -1.
Examples
The following example illustrates the clock () function is used.
#include <time.h> #include <stdio.h> int main() { clock_t start_t, end_t, total_t; int i; start_t = clock(); printf("程序启动,start_t = %ld\n", start_t); printf("开始一个大循环,start_t = %ld\n", start_t); for(i=0; i< 10000000; i++) { } end_t = clock(); printf("大循环结束,end_t = %ld\n", end_t); total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC; printf("CPU 占用的总时间:%f\n", total_t ); printf("程序退出...\n"); return(0); }
Let's compile and run the above program, which will result in the following:
程序启动,start_t = 0 开始一个大循环,start_t = 0 大循环结束,end_t = 20000 CPU 占用的总时间:0.000000 程序退出...