Sunday, July 23, 2006

Windows Timers.

Trying to make the thingy for the job at the DEI lab, I've learned a couple of things about Windows Timers. There are inf ways to run a scheduled event every x mseconds on Windows, the question is "which one is the best?". I've looked mainly at three approaches:

1) Sleep(ms) function. The easy one, it suspends the thread for that time, the Windows version of sleep() POSIX call.

2) QueueTimer. Calls a callback routine at each timer ticks.

3) WaitableTimer, each timer ticks sets an event you can wait for (WaitForSingleObject)

After a bit of testing with the 3 solutions, I saw that the two most fancy ones (last two) behaves pretty badly. Setting a wait time of 350ms, the standard deviation (after some 1000 samples) of #2 is 11.5ms and of #3 22.7ms. That is quite a lot. Surprisingly #1 did quite well, std deviation 1.5ms. (I mesured the actual delays using QueryPerformanceCounter.)

1 comment:

Radiant said...

Grande sleep(ms)! Così si scopre che talora le soluzioni più semplici sono anche le più performanti...