1. 程式人生 > >C語言計算程序中某一個函數或算法的執行時間

C語言計算程序中某一個函數或算法的執行時間

lock nis 程序 while tar 計算 stdio.h locks turn

計算程序中某一個函數或算法的執行時間

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

int main()
{
    long i = 10000000L;
    clock_t start, finish;
    double duration;
    printf( "Time to do %ld empty loops is ", i) ;
    start = clock();
    while( i-- );
    finish = clock();
    duration = (double)(finish - start) / CLOCKS_PER_SEC;
    printf( "%f seconds\n", duration );
    return 0;
}

C語言計算程序中某一個函數或算法的執行時間