Created
May 20, 2014 03:38
-
-
Save kasajian/b37c0b94d418c01bed43 to your computer and use it in GitHub Desktop.
C++ Timer for measuring performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if _DEBUG | |
file://===================================================================== | |
========= | |
// WinQueryPerformance | |
// Outputs and resets the performance counter. | |
// (For multiple timers, see CPerformanceTimer) | |
// | |
// Use it like so: | |
// | |
// WinQueryPerformance( 0 ); | |
// do some stuff | |
// WinQueryPerformance( "Compiling scripts" ); | |
// | |
// Sample output in debugger output window: | |
// Compiling scripts took .54983 seconds | |
// | |
file://===================================================================== | |
========= | |
void WinQueryPerformance( char* msg ) | |
{ | |
static LARGE_INTEGER prevCounter; | |
LARGE_INTEGER currentPerformanceCounter; | |
static double frequency; | |
QueryPerformanceCounter( ¤tPerformanceCounter ); | |
if ( !frequency ) | |
{ | |
LARGE_INTEGER performanceFrequency; | |
QueryPerformanceFrequency( &performanceFrequency ); | |
frequency = double( performanceFrequency.QuadPart ); | |
QueryPerformanceCounter( &prevCounter ); | |
} | |
// Even though under VC %i64d is supposed to work, it don't, so we use | |
doubles. | |
if ( msg ) | |
MATRACE( "%s %.7g took seconds\n", (const char*) msg, | |
ouble( currentPerformanceCounter.QuadPart - prevCounter.QuadPart ) / | |
frequency ); | |
currentPerformanceCounter = prevCounter; | |
QueryPerformanceCounter( &prevCounter ); | |
} // WinQueryPerformance | |
// | |
// The CPerformanceTimer class can be used to handle multiple timers. | |
// (For a simple single use timer, see WinQueryPerformance) | |
// | |
// Use it like so: | |
// | |
// { | |
// CPerformanceTimer timer( "Compiling scripts" ); | |
// timer.Start( ); | |
// do some stuff | |
// timer.Report( ); | |
// } | |
// | |
// Sample output in debugger output window: | |
// Compiling scripts took .54983 seconds | |
// | |
// timer.Stop() can be used to temporarily stop timer. timer.Start() can be | |
used to restart. | |
// timer.Report() stops the timer. | |
// | |
double CPerformanceTimer::fFrequency = 0; | |
int pIndentation = 0; | |
CPerformanceTimer::CPerformanceTimer( const char* msg ) | |
{ | |
// The frequency doesn't change, so get it once. | |
if ( !fFrequency ) | |
{ | |
LARGE_INTEGER performanceFrequency; | |
QueryPerformanceFrequency( &performanceFrequency ); | |
fFrequency = double( performanceFrequency.QuadPart ); | |
} | |
if ( strlen( msg ) >= sizeof( fMsg ) ) | |
WinDebugBreak( ); | |
strcpy( fMsg, msg ); | |
for ( int i = pIndentation; i; i-- ) | |
MATRACE( " " ); | |
MATRACE( "BEGIN %s\n", (const char*) fMsg ); | |
pIndentation++; | |
fTimer = 0.0; | |
fAccum = 0.0; | |
} // CPerformanceTimer::CPerformanceTimer | |
void CPerformanceTimer::Start( ) | |
{ | |
LARGE_INTEGER currentPerformanceCounter; | |
QueryPerformanceCounter( ¤tPerformanceCounter ); | |
fTimer = (double) ( currentPerformanceCounter.QuadPart ); | |
} // CPerformanceTimer::Start | |
void CPerformanceTimer::Stop( ) | |
{ | |
if ( fTimer ) | |
{ | |
LARGE_INTEGER currentPerformanceCounter; | |
QueryPerformanceCounter( ¤tPerformanceCounter ); | |
fAccum += double( currentPerformanceCounter.QuadPart - fTimer ); | |
fTimer = 0; | |
} | |
} // CPerformanceTimer::Stop | |
void CPerformanceTimer::Report( ) | |
{ | |
LARGE_INTEGER currentPerformanceCounter; | |
QueryPerformanceCounter( ¤tPerformanceCounter ); | |
this->Stop( ); | |
// Even though under VC %i64d is supposed to work, it don't, so we use | |
doubles. | |
pIndentation--; | |
for ( int i = pIndentation; i; i-- ) | |
MATRACE( " " ); | |
MATRACE( "END %s -- took %.7g seconds\n", (const char*) fMsg, fAccum / | |
fFrequency ); | |
} // CPerformanceTimer::CPerformanceTimer | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment