Created
May 29, 2014 20:07
-
-
Save csmoore/d3af490666378272d6ff to your computer and use it in GitHub Desktop.
Simple example of QueryPerformanceCounter Usage
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 needed #include Winbase.h - link: Kernel32.lib | |
double PCFreq = 0.0; | |
LONGLONG CounterStart = 0; | |
void StartCounter() | |
{ | |
LARGE_INTEGER li; | |
if (!QueryPerformanceFrequency(&li)) | |
cout << "QueryPerformanceFrequency failed!\n"; | |
PCFreq = double(li.QuadPart) / 1000.0; | |
QueryPerformanceCounter(&li); | |
CounterStart = li.QuadPart; | |
} | |
double GetCounter() | |
{ | |
LARGE_INTEGER li; | |
QueryPerformanceCounter(&li); | |
return double(li.QuadPart - CounterStart) / PCFreq; | |
} | |
void TestTimer() | |
{ | |
StartCounter(); // <--- only needs called once per run | |
cout << "Starting Timed Test" << endl; | |
double start = GetCounter(); | |
// Test long/slow artifact here: should take at least > 10us, or call many times in a loop until it does | |
double end = GetCounter(); | |
double elapsed = end - start; | |
printf(" ::: Elapsed availableKeywords(): %3.3f ms, %3.3f sec, %3.3f min\n", elapsed, | |
elapsed / 1000.0, elapsed / 1000.0 / 60.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment