一、簡介
使用clock()
函數記錄程序開始、結束時間戳。然后將開始結束時間戳差除以CLOCKS_PER_SEC
得到程序的耗用的時間(秒數)。
二、代碼示例
#include <iostream>
#include <time.h>
#include <math.h>
int main(int, char **)
{clock_t start;clock_t finish;start = clock(); // 開始計時/* do some thing */for (int i = 0; i < 10000; i++){int c = exp(2.0);}finish = clock(); // 計時結束// 打印程序耗時,單位:秒sstd::cout << (double)(finish - start) / CLOCKS_PER_SEC << std::endl;
}