家装e站质量监督电话:c 语言中clock函数怎么用啊

来源:百度文库 编辑:神马品牌网 时间:2024/04/28 21:44:04
最好详细讲

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