A High Resolution Timer for Win32

From Scriptionary.com

Jump to: navigation, search
This article encourages the reader to edit the article and provide additional information to enhance the article. This message does not denote incompletion of the text.
Author Eddy Luten
Author Link Scriptionary.com
Contributors n/a
Notes
C++, WIN32 only, no explanations provided, simply code.

This article lists the code for creating a high resolution timer for the Microsoft Windows platform. High resolution timers are often used in multimedia and entertainment applications.

Contents


Timer.h, Timer Prototype

#ifndef _TIMER_H_
#define _TIMER_H_
 
#ifndef WIN32_LEAN_AND_MEAN
#	define WIN32_LEAN_AND_MEAN
#endif
 
#include <windows.h>
 
// Defines and Creates a simple High Resolution Timer
class Timer
{
protected:
	double startTime;
	double endTime;
	bool active;
	LARGE_INTEGER frequency;
	LARGE_INTEGER startCount;
	LARGE_INTEGER endCount;
 
public:
	Timer ( void ); // ctor
	~Timer ( void ){}; // dtor
 
	void Start ( void ); // Starts timer
	void Stop ( void ); // Stops the timer
	void Reset ( void ); // Resets the timer, everything = 0
	double GetTime ( void ); // Time in Seconds
	double GetMilliTime ( void ); // Time in Milliseconds
	double GetMicroTime ( void ); // Time in Microseconds
	bool IsActive ( void ); // Is timer active?
}; // class Timer
 
#endif

Timer.cpp, Timer Source Code

Timer::Timer( void )
{
	this->active = false; // preset
	this->Reset(); // reset / populate the values
	return;
}; // ctor
 
/*************************************************/
 
void Timer::Start ( void )
{
	this->active = true;
	QueryPerformanceCounter(&this->startCount);
	return;
}; // Start
 
/*************************************************/
 
void Timer::Stop ( void )
{
	this->active = false;
	QueryPerformanceCounter(&this->endCount);
	return;
}; // Stop
 
/*************************************************/
 
void Timer::Reset ( void )
{
	if ( this->active ) this->Stop(); // we need to stop
	QueryPerformanceFrequency(&this->frequency); // poll freq
	{
		this->startCount.QuadPart =
		this->endCount.QuadPart	 =
		this->startTime =
		this->endTime = 0;
	}
	this->active = false;
	return;
}; // Reset
 
/*************************************************/
 
bool Timer::IsActive( void )
{
	return this->active;
}; // IsActive
 
/*************************************************/
 
double Timer::GetMicroTime( void )
{
	if(this->active) QueryPerformanceCounter(&endCount);
 
	this->startTime = 
		double (
			this->startCount.QuadPart * 
			( 1000000.0 / this->frequency.QuadPart )
		);
 
	this->endTime =
		double (
			this->endCount.QuadPart * 
			( 1000000.0 / this->frequency.QuadPart )
		);
 
	return double( this->endTime - this->startTime );
}; // GetTime
 
/*************************************************/
 
double Timer::GetMilliTime( void )
{
	return double( this->GetMicroTime() * 0.001 );
}; // GetMilliTime
 
/*************************************************/
 
double Timer::GetTime( void )
{
	return double( this->GetMicroTime() * 0.000001);
}; // GetTime

Timer Usage Example

#include <iostream>
#include "Timer.h"
 
int main( int argc, const char* argv[] )
{
	Timer myTime;
 
	std::cout << "Starting Timer..." << std::endl;
	myTime.Start();
 
	std::cout << "Going to sleep for 5 seconds..." << std::endl;
	Sleep( 5000 ); // or something else for a while
 
	std::cout << "Stopping Timer..." << std::endl;
	myTime.Stop();
 
	std::cout << myTime.GetTime() << " seconds passed" << std::endl;
 
	return EXIT_SUCCESS;
}; // main
Personal tools