/* CLOCK.C:
等待3秒,记录程序运行开始时间(start里)
循环600000次做运算
[a = sqrt(sqrt(16.0)); a = sqrt(sqrt(16.0));]
记录程序运算结属时间(finish里)
算出这六十万次运行时间.
*/
#include
#include
#include
#include
void sleep( clock_t wait );
void main( void )
{
long i = 600000L;
clock_t start, finish;
double duration;
double a;
/* 等三秒 */
printf( "Delay for 3 seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );
/* Measure the duration of an event. */
printf( "Time to do %ld loops is ", i );
start = clock();
while( i-- ) {
a = sqrt(sqrt(16.0)); a = sqrt(sqrt(16.0));
}
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%lf seconds\n", duration );
}
/* 等待多少毫秒的子程序 */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}