1.clock()
標准庫中ctime頭文件
#include <ctime> #include <iostream> using namespace std; int main(){ clock_t start = clock(); //獲取當前系統時間 function(); clock_t end = clock(); double programTimes = ((double) end -start) / CLOCKS_PER_SEC; }
2.high_resolution_clock
chrono (C++11新增程序庫)
#include <chrono> #include <iostream> using namespace std; int main(){ auto beginTime = std::chrono::high_resolution_clock::now(); function(); auto endTime = std::chrono::high_resolution_clock::now(); auto elapsedTime = std::chrono::duration_cast<std::chrono::microseconds>(endTime-beginTime); double programTimes = ((double) elapsedTime.count(); }