Skip to content

Instantly share code, notes, and snippets.

@PathogenDavid
Created July 10, 2014 16:14
Show Gist options
  • Save PathogenDavid/7960ecfdb4ef059f36e8 to your computer and use it in GitHub Desktop.
Save PathogenDavid/7960ecfdb4ef059f36e8 to your computer and use it in GitHub Desktop.
WaitableTimerTest2
#include <Windows.h>
#include <stdio.h>
#include <assert.h>
#define NANOSECONDS_PER_SECOND ( 1000000000LL )
#define NANOSECONDS_PER_TIMER ( 100LL )
int main()
{
LARGE_INTEGER begin;
LARGE_INTEGER end;
LARGE_INTEGER timeToWait;
int targetMisses = 0;
int targetsMet = 0;
LONGLONG averageMissAmount = 0LL;
HANDLE timer = CreateWaitableTimer(NULL, TRUE, NULL);
assert(timer != NULL);
timeToWait.QuadPart = -10000LL; //10000 * 100 nanoseconds = 1 ms
LARGE_INTEGER performanceCounterFrequency;//On my machine: 3312841, or 301.9 ns
assert(QueryPerformanceFrequency(&performanceCounterFrequency));
LONGLONG maxDelta = -timeToWait.QuadPart * NANOSECONDS_PER_TIMER * performanceCounterFrequency.QuadPart / NANOSECONDS_PER_SECOND;
printf("timeToWait: %I64d ns\n", -timeToWait.QuadPart * NANOSECONDS_PER_TIMER);
printf("maxDelta : %I64d ticks\n", maxDelta);
printf("frequency : %I64d ticks/second\n", performanceCounterFrequency.QuadPart);
for( int i = 0; i < 100000; i++ )//100,000 iterations
{
assert(QueryPerformanceCounter(&begin));
assert(SetWaitableTimer(timer, &timeToWait, 0, NULL, NULL, TRUE));
assert(WaitForSingleObject(timer, INFINITE) == WAIT_OBJECT_0);
assert(QueryPerformanceCounter(&end));
LONGLONG delta = end.QuadPart - begin.QuadPart;
if (delta > maxDelta)
{
targetMisses++;
averageMissAmount += delta - maxDelta;
}
else
{ targetsMet++; }
}
printf("Targets missed: %d\n", targetMisses);
printf("Targets met : %d\n", targetsMet);
averageMissAmount /= (LONGLONG)targetMisses;
printf("Average miss amount: %I64d ticks\n", averageMissAmount);
printf("Average miss amount: %I64d ns\n", (averageMissAmount * NANOSECONDS_PER_SECOND) / performanceCounterFrequency.QuadPart);
printf("Press any key to exit...");
getchar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment