Click here to Skip to main content
15,892,072 members
Articles / Programming Languages / C++

Precise duration measurement

Rate me:
Please Sign up or sign in to vote.
4.11/5 (25 votes)
27 Dec 1999 120.7K   2.7K   44  
A simple class that provides high precision timing.
#pragma once

class CDuration
{
protected:
	LARGE_INTEGER m_liStart;
	LARGE_INTEGER m_liStop;

	LONGLONG m_llFrequency;
	LONGLONG m_llCorrection;

public:
	CDuration(void);

	void Start(void);
	void Stop(void);
	double GetDuration(void) const;
};

inline CDuration::CDuration(void)
{
	LARGE_INTEGER liFrequency;

	QueryPerformanceFrequency(&liFrequency);
	m_llFrequency = liFrequency.QuadPart;

	// Calibration
	Start();
	Stop();

	m_llCorrection = m_liStop.QuadPart-m_liStart.QuadPart;
}

inline void CDuration::Start(void)
{
	// Ensure we will not be interrupted by any other thread for a while
	Sleep(0);
	QueryPerformanceCounter(&m_liStart);
}

inline void CDuration::Stop(void)
{
	QueryPerformanceCounter(&m_liStop);
}

inline double CDuration::GetDuration(void) const
{
	return (double)(m_liStop.QuadPart-m_liStart.QuadPart-m_llCorrection)*1000000.0 / m_llFrequency;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions