耗时统计
简单统计
#include<Windows.h> DWORD startTime = GetTickCount();//计时开始 ...代码块 DWORD endTime = GetTickCount();//计时结束 cout << "耗时:" << endTime - startTime << "ms" << endl;
或
#include<Windows.h> LARGE_INTEGER freq, start, end; int main() { QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&start); //...代码块 QueryPerformanceCounter(&end); double cost = (end.QuadPart - start.QuadPart); cost = (cost * 1000 / freq.QuadPart); //ms return 0; }
精确统计
#include<chrono> auto start=std::chrono::high_resolution_clock::now(); ...代码块 auto end = std::chrono::high_resolution_clock::now(); cout << "耗时:" << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << endl;
OpenCV方式:
double t = (double)getTickCount(); // 需要被测量的程序段 t = ((double)getTickCount() - t)/getTickFrequency(); cout << "执行时间(秒): " << t << endl;