Skip to content

Instantly share code, notes, and snippets.

@rdp
Created August 18, 2011 01:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdp/1153062 to your computer and use it in GitHub Desktop.
Save rdp/1153062 to your computer and use it in GitHub Desktop.
example use of QueryPerformanceCounter
long double PCFreqMillis = 0.0;
// this call only needed once...
// who knows if this is useful or not, speed-wise...
void WarmupCounter()
{
LARGE_INTEGER li;
BOOL ret = QueryPerformanceFrequency(&li);
assert(ret != 0); // only gets run in debug mode LODO
PCFreqMillis = (long double(li.QuadPart))/1000.0;
}
__int64 StartCounter()
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return (__int64) li.QuadPart;
}
long double GetCounterSinceStartMillis(__int64 sinceThisTime)
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
assert(PCFreq!= 0.0);
return long double(li.QuadPart-sinceThisTime)/PCFreqMillis; //division kind of forces us to return a double of some sort...
} // LODO do I really need long double here? no really.
// use like
// __int64 start = StartCounter();
// ...
// long double elapsed = GetCounterSinceStartMillis(start)
// printf("took %.020Lf ms", elapsed);
// or to deeply debug: printf("start %I64d end %I64d took %.020Lf ms", start, StartCounter(), elapsed);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment