在C中測試函數運行時間

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

clock_t start, stop;    //clock_t爲clock()返回的變量類型
double duration;        //記錄被測函數運行時間,以秒爲單位

int main(int argc, char **argv)
{
    /* 再也不測試範圍內的準備工做寫在clock()調用以前 */

    //開始計時
    start = clock();
    //被測量的函數

    //中止計時
    stop = clock();
    //計算運行時間
    duration = ((double)(stop - start)) / CLOCKS_PER_SEC;

    /* 其餘不在測試範圍的處理寫在後面, 例如輸出duration的值 */
    printf("Running time: %6.2es.\n", duration);
    return 0;
}
相關文章
相關標籤/搜索