C使用clock計算代碼執行時間

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int i;
    time_t start, stop;
    double duration;
    start = clock();

    long long sum=0;
    for(int i=0;i<1e2;i++){
        sum=sum+i;
        printf("%d,%lld\n",i+1,sum);
    }

    stop =  clock();
    duration = ((double)(stop - start))/CLOCKS_PER_SEC;


    printf("\n duration = %f,%f\n",duration,CLOCKS_PER_SEC);
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章