1. 程式人生 > >opencv測試執行時間

opencv測試執行時間

每次需要測試執行時間的時候都要再去網上搜索一遍,基於clock的是秒級別的,精確度不高,我之前一直用基於頻率計數器的,但是一直用沒有封裝好的函式,把這個記錄下來也是圖個方便。

需要包含的標頭檔案和namespace是

#include <Windows.h>
#include <iostream>
using namespace std;

類的定義如下

class CTimer
{
public:
	CTimer()
	{
		QueryPerformanceFrequency(&m_Frequency);
		Start();
	}
	void Start()
	{
		QueryPerformanceCounter(&m_StartCount);
	}
	double End()
	{
		LARGE_INTEGER CurrentCount;
		QueryPerformanceCounter(&CurrentCount);
		return double(CurrentCount.LowPart - m_StartCount.LowPart) / (double)m_Frequency.LowPart;
	}
	void ShowNow()
	{
		LARGE_INTEGER CurrentCount;
		QueryPerformanceCounter(&CurrentCount);
		cout<<"Timer Count is:"<<double(CurrentCount.LowPart - m_StartCount.LowPart) / (double)m_Frequency.LowPart<<endl;
	}
private:
	LARGE_INTEGER m_Frequency;
	LARGE_INTEGER m_StartCount;
};


在程式執行的時候這樣使用

int main()
{
  CTimer t; // 初始化
  sleep(1000);// 這段是具體的程式中的內容
  cout<<"Detection time is "<<t.End()*1000<<"ms"<<endl; // 顯示時間
  return 0;
}

結束之後就可以顯示時間了。