c 语言中clock函数怎么用啊

发布网友

我来回答

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() )
;
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com