Opencv计算算法运行时间
最初的C接口是 cvGetTickCount()
和cvGetTickFrequency()
,但注意,此时得到的单位是us级的统计时间。使用如下:
| double t = (double)cvGetTickCount();
t = (double)cvGetTickCount() - t; printf( "run time = %gms\n", t/(cvGetTickFrequency()*1000) ); printf( "run time = %gs\n", t/(cvGetTickFrequency()*1000000) );
|
在Opencv到了2.x之后在命名空间cv中又多了几个函数:
getTickCount()
:返回CPU自某个时间(如启动电脑)以来走过的时钟周期数。
getTickFrequency()
返回CPU一秒中所走的时钟周期数。所以可以以秒为单位对某运算时间计时。
1 2 3
| double start = static_cast<double>(getTickCount()); double time = ((double)getTickCount() - start) / getTickFrequency(); cout << "所用时间为:" << time << "秒" << endl;
|
同时C++接口中还将上面的函数封装为了一个类TickMeter,使用起来比较方便,示例:
1 2 3 4 5 6 7 8 9 10 11
| #include<opencv2/contrib/contrib.hpp>
TickMeter tm; tm.start();
tm.stop(); cout << tm.getCounter() << endl; cout << tm.getTimeMicro() << endl; cout << tm.getTimeMilli() << endl; cout << tm.getTimeSec() << endl; cout << tm.getTimeTicks() << endl;
|