有些時候我們在C++編程時需要統計和比較一些函數或者語句的執行時間,從而判斷算法的效率。下面是其中一種方法:
#include <iostream>
#include<time.h>
int main()
{
clock_t one, two;
one = clock(); // 開始計時
... 要統計時間的代碼
two= clock(); // 結束計時
cout << "Runtime: " << (double)(two- one ) * 1000.0 / CLOCKS_PER_SEC << " ms!" << endl;
}