将执行时间代码放入一个函数,OpenCV? [英] Putting execution timing code into a function, OpenCV?
问题描述
我有这个代码片段,我在我的程序(C++ 和 OpenCV)中随处使用.它用于计时一些操作:
I have this code snippet that I use everywhere in my programs (C++ and OpenCV). It is for timing some operations:
double t;
// Some code...
t = (double)getTickCount();
Object1.LotOfComputing();
t = 1000*((double)getTickCount() - t)/getTickFrequency();
cout << "Time for LotOfComputing =" << t << " milliseconds."<< endl;
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
这是 OpenCV 文档推荐的对代码的函数/部分进行计时的方式,并且在使用它几周后似乎对我有用.我测量的时间大约从 1 毫秒到 700 毫秒不等,我将它们四舍五入到毫秒.
This is how the OpenCV doc recommend to time a function/portion of code and it seems to works for me after using it for some weeks. The times I measure are ranging from about 1ms to 700ms, and I round them to the millisecond.
问题是我在我的程序中对很多不同的操作进行计时,并且代码被这些片段弄得杂乱无章.
The problem is that I time a lot of different operations in my program and the code is cluttered by these snippets.
我想知道将这些代码行放入函数中是否明智,例如:
I wonder if it would be wise to put these lines of code into a function, like:
double timing(double time){
timing = 1000*((double)getTickCount() - time)/getTickFrequency();
cout << "Time for LotOfComputing =" << timing << " milliseconds."<< endl;
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
return timing;
}
所以我可以使用它:
double t;
// Some code...
t = (double)getTickCount();
Object1.LotOfComputing();
timing(t);
我只是关心通过函数调用的执行时间......也许我只是无所事事!
I am simply concerned about the execution time of going through function calls... Maybe I am simply worrying for nothing!
推荐答案
这里有一个分析器的小魔法 ;)
基本上,它为每个范围创建静态计数器(想想一对 {}),在模块启动时初始化,并在模块发布时读取.当然,这也需要时间,所以你不希望这在一些紧密的循环中.
here's a little profiler magic ;)
basically, it makes static counters per scope (think a pair of {}), which get initialized at module startup, and get read out at module release time. of course this takes time, too, so you don't want this inside some tight loop.
// you might want to change the clock
#define get_ticks cv::getTickCount
#define get_freq cv::getTickFrequency
double dt(int64 t) { return double(t*1000/get_freq())/1000.0; }
struct Profile
{
string name;
int64 t; // accumulated time
int64 c; // function calls
Profile(const string & name)
: name(name)
, t(0)
, c(0)
{}
~Profile()
{
cerr << format("%-24s %8u ",name.c_str(),c);
cerr << format("%13.6f ",dt(t/c));
cerr << format("%13.6f ",dt(t));
cerr << format("%14u",t);
cerr << endl;
}
struct Scope
{
Profile & p;
int64 t;
Scope(Profile & p)
: p(p)
, t(get_ticks())
{}
~Scope()
{
int64 t1 = get_ticks();
if ( t1 > t )
{
p.t += t1 - t;
p.c ++;
}
}
};
};
#define PROFILEX(s) static Profile _a_rose(s); Profile::Scope _is_a_rose_is(_a_rose);
#define PROFILE PROFILEX(__FUNCTION__)
稍后使用:
void foo() {
PROFILE;
int k=10;
while (k) {
PROFILEX("foo while loop");
k --;
}
}
void bar() {
PROFILE;
}
int main() {
PROFILE;
{
PROFILEX("useless scope");
bar();
bar();
}
foo();
return 0;
}
<小时>
foo while loop 10 0.000000 0.000002 3959
foo 1 0.000007 0.000007 13000
bar 2 0.000000 0.000000 843
useless scope 1 0.000004 0.000004 7710
main 1 0.000017 0.000017 31712
这篇关于将执行时间代码放入一个函数,OpenCV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!