i use my super accurate timer class (note that it MIGHT lose some very very tiny accuracy in pausing but generally, nothing to even bother thinking about), if anyone wants it:
//
//TIMER.H
//
//header guards
#ifndef _HX_11_TIMERS_H_
#define _HX_11_TIMERS_H_
#define HX_TIMR_TYPE float
#define HX_TIMER_SECONDS 1
#define HX_TIMER_MINUTES (1.0f/60.0f)
#define HX_TIMER_HOURS ((1.0f/60.0f)/60.0f)
#define HX_TIMER_MILLISECONDS 1000
#define HX_TIMER_MICROSECONDS 1000000
/*********************************************************************
*********************************hxTimer*****************************
Timers class
*********************************************************************/
class hxTimerImpl;
class hxTimer
{
friend class hxTimerImpl;
protected:
hxTimerImpl* __Impl__;
public:
hxTimer ( float fUnit = HX_TIMER_MILLISECONDS );
~hxTimer ( void );
void Start ( void );
void Pause ( void );
void Reset ( void );
HX_TIMR_TYPE GetElapsedTime ( void ) const;
};
#endif //_HX_11_TIMERS_H_
//
//TIMER.cpp
//
#include "Timer.h"
float _SPC = 0;
class hxTimerImpl
{
__int64 _startTime;
__int64 _pauseStartTime;
__int64 _totalPauseTime;
float _unit;
HX_TIMR_TYPE _GetCurrentTime ( void ) const
{
//calculate elapsed time
__int64 currtime = 0;
QueryPerformanceCounter ( (LARGE_INTEGER*)&currtime );
return currtime;
};
HX_TIMR_TYPE GetPauseTime ( void ) const
{
if ( _pauseStartTime )
return 0;
return _GetCurrentTime ( ) - _pauseStartTime;
};
public:
hxTimerImpl ( float fUnit )
{
_unit = fUnit;
//initialize accurate timing system
if ( _SPC == 0 )
{
__int64 countsPerSec;
QueryPerformanceFrequency ( (LARGE_INTEGER*)&countsPerSec );
_SPC = 1.0f / (float)countsPerSec;
}
Reset ( );
};
~hxTimerImpl ( void )
{
};
void Start ( void )
{
_totalPauseTime += GetPauseTime ( );
_pauseStartTime = -1;
};
void Pause ( void )
{
_pauseStartTime = _GetCurrentTime ( );
};
void Reset ( void )
{
_startTime = _GetCurrentTime ( );
_totalPauseTime = 0;
Pause ( );
};
HX_TIMR_TYPE GetElapsedTime ( void ) const
{
HX_TIMR_TYPE totalElapsedTime = _GetCurrentTime ( ) - _startTime;
return ((totalElapsedTime - (_totalPauseTime + GetPauseTime ( )))*_SPC)*_unit; //multiply to convert unit
};
};
hxTimer::hxTimer ( float fUnit ) : __Impl__ ( new hxTimerImpl ( fUnit ) )
{
}
hxTimer::~hxTimer ( void )
{
HX_SAFE_DELETE ( __Impl__ );
}
void hxTimer::Start ( void )
{
__Impl__->Start ( );
}
void hxTimer::Pause ( void )
{
__Impl__->Pause ( );
}
void hxTimer::Reset ( void )
{
__Impl__->Reset ( );
}
HX_TIMR_TYPE hxTimer::GetElapsedTime ( void ) const
{
return __Impl__->GetElapsedTime ( );
}
usage is very easy:
hxTimer tmr;
tmr.Start ( ); //starts the timer
tmr.Pause ( ); //pauses the timer
tmr.Reset ( ); //resets the timer and pauses it
tmr.GetElapsedTime ( ); //gets the time since the last start, pause time is calculated automatically
and if you want to use a specific unit (minutes, hours, seconds, etc..)
hxTimer tmr ( HX_TIMER_MINUTES );
tmr.Start ( );
//do some stuff here..
float fElapsedTimeInMinutes = tmr.GetElapsedTime ( );
also - if you will use some small unit, like microseconds or nanoseconds or whatever, make sure you change
#define HX_TIMR_TYPE float
to
#define HX_TIMR_TYPE __int64
because float will be too small to handle large numbers, and you won't need floating points in micro or nano seconds..
also one final note: the code may look messy or stupid, that's because it uses pImpl idiom, i really prefer to have my code written using it, especially when compiling as a .lib, but it's up to you, and you can do some minor changes to make it normal
EDIT: whoops, forgot some includes, added in the .h, i also forgot that you will need to call
//begin accurate timing system
timeBeginPeriod ( 1 );
at the beginning and
//end accurate timing system
timeEndPeriod ( 1 );
at the end of your program