首页 > 技术文章 > c 计算 语句 执行 时间

chunlifang-luck 2013-07-08 23:46 原文

当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:
 
#include “stdio.h”
#include “stdlib.h”
#include “time.h”
 
int main( void )
{
   long    i = 10000000L;
   clock_t start, finish;
   double  duration;
   /* 测量一个事件持续的时间*/
   printf"Time to do %ld empty loops is ", i );
   start = clock();
   while( i-- )      ;
   finish = clock();
   duration = (double)(finish - start) / CLOCKS_PER_SEC;
   printf"%f seconds\n", duration );
   system("pause");
}
 
在笔者的机器上,运行结果如下:
 
Time to do 10000000 empty loops is 0.03000 seconds

推荐阅读