clock_t用法

1547 ワード

clock_t定義
#ifndef _CLOCK_T_DEFINED
typedef long clock_t;
#define _CLOCK_T_DEFINED
#endif

clock_tは長い整形数です.time.hファイルには、定数CLOCKS_も定義されています.PER_SECは、1秒に何個のクロックタイミングユニットがあるかを表すために使用され、以下のように定義されています.
#define CLOCKS_PER_SEC ((clock_t)1000)

clock()の戻り単位はミリ秒です.秒単位でご利用いただけます
duration = (finish - start) / CLOCKS_PER_SEC;
#include 
#include 
#include 

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
"
, duration ); system("pause"); }