我有这个代码片段,我在我的程序(C++和OpenCV)中到处使用.它用于计时某些操作:
double t;
// Some code...
t = (double)getTickCount();
Object1.LotOfComputing();
t = 1000*((double)getTickCount() - t)/getTickFrequency();
cout << "Time for LotOfComputing =" << t << " milliseconds."<< endl;
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
Run Code Online (Sandbox Code Playgroud)
这就是OpenCV doc建议对代码的功能/部分进行计时的方式,它在使用几周后似乎对我有用.我测量的时间从大约1毫秒到700毫秒,我将它们舍入到毫秒.
问题是我在程序中计算了很多不同的操作,代码被这些片段弄得乱七八糟.
我想知道将这些代码行放入函数是否明智,例如:
double timing(double time){
timing = 1000*((double)getTickCount() - time)/getTickFrequency();
cout << "Time for LotOfComputing =" << timing << " milliseconds."<< endl;
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
return timing;
}
Run Code Online (Sandbox Code Playgroud)
所以我可以使用这个:
double t;
// Some code...
t = (double)getTickCount();
Object1.LotOfComputing();
timing(t);
Run Code Online (Sandbox Code Playgroud)
我只关心通过函数调用的执行时间......也许我只是在担心什么都没有!