本文共 861 字,大约阅读时间需要 2 分钟。
计算程序执行所需的时间:
clock_t clock( void );
clock_t start,finish;
#include输出结果为:#include #include void sleep( clock_t wait );void main( void ){ long i = 600000L; clock_t start, finish; double duration; /* Delay for a specified time. */ printf( "Delay for three seconds\n" ); sleep( (clock_t)3 * CLOCKS_PER_SEC ); printf( "Done!\n" ); /* Measure the duration of an event. */ printf( "Time to do %ld empty loops is ", i ); start = clock(); while( i-- ) ; finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf( "%2.1f seconds\n", duration );}/* Pauses for a specified number of milliseconds. */void sleep( clock_t wait ){ clock_t goal; goal = wait + clock(); while( goal > clock() ) ;}
Delay for three secondsDone!Time to do 600000 empty loops is 0.1 seconds
转载地址:http://vbsvi.baihongyu.com/