OpenCV 利用getTickCount()与getTickFrequency()计算执行时间

来源:互联网 发布:阿里云虚拟机价 价格 编辑:程序博客网 时间:2024/05/23 09:58

其实这是个很简单的应用,贴出来是因为我经常能用到这两个函数,顺便写一下吧。

double t1 = (double)getTickCount();...double t2 = (double)getTickCount();cout<<"time:"<<(t2-t1)*1000/getTickFrequency()<<endl;

getTickCount()与getTickFrequency()都被定义在core.hpp文件下:

//! Returns the number of ticks./*!  The function returns the number of ticks since the certain event (e.g. when the machine was turned on).  It can be used to initialize cv::RNG or to measure a function execution time by reading the tick count  before and after the function call. The granularity of ticks depends on the hardware and OS used. Use  cv::getTickFrequency() to convert ticks to seconds.*/CV_EXPORTS_W int64 getTickCount();/*!  Returns the number of ticks per seconds.  The function returns the number of ticks (as returned by cv::getTickCount()) per second.  The following code computes the execution time in milliseconds:  \code  double exec_time = (double)getTickCount();  // do something ...  exec_time = ((double)getTickCount() - exec_time)*1000./getTickFrequency();  \endcode*/CV_EXPORTS_W double getTickFrequency();/*!  Returns the number of CPU ticks.  On platforms where the feature is available, the function returns the number of CPU ticks  since the certain event (normally, the system power-on moment). Using this function  one can accurately measure the execution time of very small code fragments,  for which cv::getTickCount() granularity is not enough.*/CV_EXPORTS_W int64 getCPUTickCount();

getTickCount():用于返回从操作系统启动到当前所经的计时周期数,看名字也很好理解,get Tick Count(s)。
getTickFrequency():用于返回CPU的频率。get CPU Tick Count(s)。这里的单位是秒。

所以剩下的就很清晰了:
总次数/一秒内重复的次数 = 时间(s)
总次数*1000/一秒内重复的次数 = 时间(ms)

2 1
原创粉丝点击