25. Compute time cost


文章作者: cyclezone
版权声明: 本文为博主「cyclezone」的原创文章,采用 CC BY 4.0 许可协议。转载请附上原文出处链接和本声明 cyclezone !

25. Compute time cost


int main()
{
	{
		CHTime timecost;
		fun1();
	}
	
	{
		CHTime timecost;
		fun2();
		timecost.getTime();
	}
}

////**********************CHTime.h************************

#ifdef WIN32
/// for windows
#include <windows.h> 
#else //BUTTER_LINUX
//// for linux
#include <sys/time.h> 
#endif

class CHTime
{
public:
	CHTime(){ 
		Begin();
	}
	~CHTime(){
		End();
	}
	void Begin()
	{
#ifdef WIN32
       QueryPerformanceFrequency(&m_freq);
       QueryPerformanceCounter(&m_queryBegin);
#else
       gettimeofday(&m_begin,NULL);
#endif
	}
	void End()
	{
#ifdef WIN32   
       QueryPerformanceCounter(&m_queryEnd);
       m_timeuse=(double)(m_queryEnd.QuadPart-m_queryBegin.QuadPart)/(double)m_freq.QuadPart * 1000.0; 
#else
       gettimeofday(&m_end,NULL);
       m_timeuse = (m_end.tv_sec - m_begin.tv_sec) + (double)(m_end.tv_usec - m_begin.tv_usec)/1000.0;
#endif
      cout<<"timecost = "<<m_timeuse<<endl;  //(unit:ms)
	}
	double getTime()
	{
		End(); 
		return m_timeuse;
	}
protected:	
    double m_timeuse;///ms

#ifdef WIN32
    LARGE_INTEGER m_queryBegin,m_queryEnd,m_freq;
#else
	struct timeval m_begin,m_end;
#endif

}

文章作者: cyclezone
版权声明: 本文为博主「cyclezone」的原创文章,采用 CC BY 4.0 许可协议。转载请附上原文出处链接和本声明 cyclezone !

评论