Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Precise duration measurement

, 27 Dec 1999
A simple class that provides high precision timing.
duration_demo.zip
DurationTest
DurationTest.dsp
DurationTest.dsw
duration_src.zip
#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

About the Author

Laurent Guinnard

Switzerland Switzerland
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 28 Dec 1999
Article Copyright 1999 by Laurent Guinnard
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid