项目中需要测试函数的运行时间,加入计时程序。简化实现如下:
clock()记录开始和结束时间,CLOCKS_PER_SEC表示时钟次数,定义如下:
#define CLOCKS_PER_SEC ((clock_t)1000)
代码如下:
#include <time.h>
#include <stdio.h>
int main()
{
double start, end, cost;
start = clock();
int i = 100000000;
while (i > 0)
{
i--;
}
end = clock();
cost = (end - start)/CLOCKS_PER_SEC;
printf("%f\n", cost);
return 0;
}
运行结果:
0.177000