Click here to Skip to main content
15,896,726 members
Articles / Web Development / HTML

Quantifying The Accuracy Of Sleep

Rate me:
Please Sign up or sign in to vote.
4.83/5 (28 votes)
20 Mar 2003CPOL12 min read 129.6K   921   34  
An analysis of actual sleep time caused by Sleep(), particularly for multithreaded applications
// StopWatch.h: interface for the CStopWatch class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STOPWATCH_H__7E8AC6E0_4649_11D7_A1EE_00C0F03D7695__INCLUDED_)
#define AFX_STOPWATCH_H__7E8AC6E0_4649_11D7_A1EE_00C0F03D7695__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CStopWatch  
{
public:
	CStopWatch();
	virtual ~CStopWatch();
	void Start(void);
	void Stop(void);
	DWORD GetLapTime() const;	// in whole microseconds (less than 214 secs) -- stopwatch keeps running
	DWORD GetInterval() const;	// in whole microseconds (less than 214 secs) -- must call Stop() first, or returns zero
	LONGLONG GetLapTimeLongLong() const;	// in whole microseconds -- stopwatch keeps running
	LONGLONG GetIntervalLongLong() const;	// in whole microseconds -- must call Stop() first, or returns zero

protected:
	LARGE_INTEGER m_liStart;
	LARGE_INTEGER m_liStop;
	LONGLONG m_llFrequency;
};


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

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

	m_liStart.QuadPart = 0;
	m_liStop.QuadPart = 0;
}


inline CStopWatch::~CStopWatch()
{

}


inline void CStopWatch::Start(void)
{
	::QueryPerformanceCounter(&m_liStart);
	m_liStop = m_liStart;
}


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


inline DWORD CStopWatch::GetInterval(void) const
{
	return (DWORD)( (m_liStop.QuadPart - m_liStart.QuadPart) * 1000000 / m_llFrequency);
}


inline DWORD CStopWatch::GetLapTime(void) const
{
	LARGE_INTEGER liCurrent;
	::QueryPerformanceCounter(&liCurrent);
	return (DWORD)( (liCurrent.QuadPart - m_liStart.QuadPart) * 1000000 / m_llFrequency);
}


inline LONGLONG CStopWatch::GetIntervalLongLong(void) const
{
	return (m_liStop.QuadPart - m_liStart.QuadPart) * 1000000 / m_llFrequency;
}


inline LONGLONG CStopWatch::GetLapTimeLongLong(void) const
{
	LARGE_INTEGER liCurrent;
	::QueryPerformanceCounter(&liCurrent);
	return (liCurrent.QuadPart - m_liStart.QuadPart) * 1000000 / m_llFrequency;
}

#endif // !defined(AFX_STOPWATCH_H__7E8AC6E0_4649_11D7_A1EE_00C0F03D7695__INCLUDED_)

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Mike O'Neill is a patent attorney in Southern California, where he specializes in computer and software-related patents. He programs as a hobby, and in a vain attempt to keep up with and understand the technology of his clients.

Comments and Discussions