- class HighResElapsedTimer
- {
- public:
- HighResElapsedTimer(const std::string& strName) : m_strName(strName)
- {
- if (m_llFrequency == 0) // frequency value not yet set
- {
- LARGE_INTEGER liFrequency;
- QueryPerformanceFrequency(&liFrequency);
- m_llFrequency = liFrequency.QuadPart;
- }
- QueryPerformanceCounter(&m_llCounter);
- }
- virtual ~HighResElapsedTimer()
- {
- LARGE_INTEGER liNow;
- QueryPerformanceCounter(&liNow);
- double duration = (double)
- (liNow.QuadPart - m_llCounter.QuadPart)
- /
- m_llFrequency;
- std::cout << m_strName.c_str() <<
- " : Elapsed time = " << duration << " microseconds" << std::endl;
- }
- private:
- std::string m_strName;
- LARGE_INTEGER m_llCounter;
- private:
- static LONGLONG m_llFrequency; // Frequency setting is based hardware clock which doesn't
- // does change - so we want to set this only once
- private:
- HighResElapsedTimer(const HighResElapsedTimer&);
- HighResElapsedTimer& operator=(const HighResElapsedTimer&);
- };
- //该片段来自于http://www.codesnippet.cn/detail/220320132511.html
来源: http://www.codesnippet.cn/detail/220320132511.html