clock_t用法

来源:互联网 发布:淘宝直播刷人软件下载 编辑:程序博客网 时间:2024/06/15 19:04

clock_t定义

#ifndef _CLOCK_T_DEFINEDtypedef long clock_t;#define _CLOCK_T_DEFINED#endif

clock_t是一个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:

#define CLOCKS_PER_SEC ((clock_t)1000)

clock()返回单位是毫秒。如果想返用秒为单位可以用

duration = (finish - start) / CLOCKS_PER_SEC;
#include <stdio.h>#include <stdlib.h>#include <time.h>int main(void){    long i = 10000000L;    double duration;    clock_t start, finish;    start = clock();    while( i-- );    finish = clock();    duration = (double)(finish - start) / CLOCKS_PER_SEC;    printf( "%f seconds\n", duration );    system("pause");}
0 0
原创粉丝点击