经常测试代码的运行耗时,以下代码可以实现
1. clock()方法,该方法时间精度为毫秒。
#include <time.h> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { clock_t startTime,endTime; startTime = clock(); {***}; endTime = clock(); cout<<"cost time(ticks): "<<endTime-startTime<<endl; }
2. time()方法
time_t st, et; st = time(NULL); // processing code et = time(NULL); cout<<"cost time(seconds):"<<difftime(et,st)<<endl;
该方法的时间精度为秒
OpenCV中的cvGetTickCount()方法
// include // code int64 st, et; st = cvGetTickCount(); // processing code et = cvGetTickCount(); printf("cost time: %f\n", (et-st)/(double)cvGetTickFrequency()/1000.0);
该方法时间精度最高。