C++ 之计时函数总结
1. clock()
#include <time.h> void main()
{clock_t start, end;start = clock();fun(); end = clock(); float t_c=float(end-start)/CLOCKS_PER_SEC;cout << "func cost = " << t_c << "ms" << endl; printf("func cost = %.3f ms", t_c);
}
2. cv::getTickCount()
#include<iostream>
#include<opencv2/opencv.hpp>void main()
{double t1 = cv::getTickCount();func();double t2 = cv::getTickCount();double t_c = (t2 - t1)/cv::getTickFrequency();cout << "func cost = " << t_c << "ms" << endl; printf("func cost = %.3f ms", t_c);
}
3. std::chrono
#include <iostream>
#include <chrono>
void time4()
{auto start = std::chrono::high_resolution_clock::now();fun(); auto end = std::chrono::high_resolution_clock::now();auto duration = std::chrono::duration_cast<std::chrono::microseconds> (end - start);cout << "elapsed time in chrono = " << duration.count()/1000.0 << "ms" << endl;
}