利用clock(),CLOCKS_PER_SEC 测试函数运行时间
clock()是C/C++中的计时函数,函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数
CLOCKS_PER_SEC表示一秒钟内CPU运行的时钟周期数(时钟计时单元)
头文件:#include< ctime > 或者 #include<time.h>
#include <stdio.h>
#include<time.h>
int main()
{
int i = 0;
time_t startTime,endTime;
startTime = clock();
while(i<100){
printf("The file is %s.\n",__FILE__);
printf( "The date is %s.\n", __DATE__ );
printf( "The time is %s.\n", __TIME__ );
printf( "This is line %d.\n", __LINE__ );
printf( "This function is %s.\n", __FUNCTION__ );
i++;
}
endTime = clock();
double totalTime = (double)(endTime-startTime)/CLOCKS_PER_SEC;
printf( "打印数据100条耗时:%f \n", totalTime);
return 0;
}
结果: