#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
在标准C/C++中,最小的计时单位是一毫秒
C计算程序运行时间
最新推荐文章于 2023-10-04 16:46:21 发布