发布网友
共1个回答
热心网友
/* CLOCK.C:
等待3秒,记录程序运行开始时间(start里)
循环600000次做运算
[a = sqrt(sqrt(16.0)); a = sqrt(sqrt(16.0));]
记录程序运算结属时间(finish里)
算出这六十万次运行时间.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
void sleep( clock_t wait );
void main( void )
{
long i = 600000L;
clock_t start, finish;
double ration;
double a;
/* 等三秒 */
printf( "Delay for 3 seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );
/* Measure the ration 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();
ration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%lf seconds\n", ration );
}
/* 等待多少毫秒的子程序 */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}