C/C++高精度计时器类

http://www.cnblogs.com/mfryf/archive/2012/02/13/2349360.html

http://blog.sina.com.cn/s/blog_4b7fad7a0100wj60.html


/*!
 * 高精度计时器, 可用于性能测试
 *
 * 使用方法:
 * CStopwatch stopwatch;
 * //在此处执行待测试的代码...
 * //获得代码执行时间(单位ms)
 * __int64 qwElapsedTime = stopwatch.Now();
 *
 */
class CStopwatch {
public:
	CStopwatch() 
	{ 
		QueryPerformanceFrequency(&m_liPerfFreq);
		Start();
	}

	void Start() 
	{
		QueryPerformanceCounter(&m_liPerfStart);
	}

	// 返回自Start调用以来的毫秒数
	__int64 Now() const 
	{ 
		LARGE_INTEGER liPerfNow;
		QueryPerformanceCounter(&liPerfNow);
		return (((liPerfNow.QuadPart - m_liPerfStart.QuadPart)*1000)/m_liPerfFreq.QuadPart);
	}


	// 返回自Start调用以来的微秒数
	__int64 NowInMicro() const 
	{ 
		LARGE_INTEGER liPerfNow;
		QueryPerformanceCounter(&liPerfNow);
		return (((liPerfNow.QuadPart - m_liPerfStart.QuadPart)*1000000)/m_liPerfFreq.QuadPart);
	}

private:
	LARGE_INTEGER m_liPerfFreq;     // Counts per second
	LARGE_INTEGER m_liPerfStart;	// Starting count
};


你可能感兴趣的:(C/C++)