13

I am currently writing an application in C# where I need to fire a timer approx. every 5 milliseconds. From some research it appears the best way to do this involves p/invoking timeBeginPeriod(...) to change the resolution of the system timer. It works well enough in my sample code.

I found an interesting warning about using this function on Larry Osterman's MSDN Blog in this entry:

Adam: calling timeBeginPeriod increases the accuracy of GetTickCount as well.

using timeBeginPeriod is a hideously bad idea in general - we've been actively removing all of the uses of it in Windows because of the power consumption consequences associated with using it.

There are better ways of ensuring that your thread runs in a timely fashion.

Does anyone know exactly why this occurs, or what those "better ways" (which are unspecified in the thread) might be? How much extra power draw are we talking about?

Chuu
  • 4,301
  • 2
  • 28
  • 54
  • By the way, you won't get more than 15.6ms - on the 'best' machine. Most often it will be 30ms. Not sure if HPET has an effect, so just be very wary of relying on Windows timer stuff. – Jonathan Dickinson Sep 28 '11 at 23:12
  • 2
    Larry knows, he wrote that code. And sure, instead of every 1/64 second, the processor is woken up from its HLT state every millisecond. 64 times less efficient. Nobody ever calls timeBeginPeriod(10). – Hans Passant Sep 29 '11 at 00:14

2 Answers2

16

Because it causes more CPU usage. A good explanation is at Timers, Timer Resolution, and Development of Efficient Code.

alldayremix
  • 727
  • 8
  • 20
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
5

Changing the system timer resolution does impact on power usage, mainly because lots of developers do not understand windows timers. You see lots of code with sleep or timer values less than 15ms. It also changes the behaviour of system tasks which can result in more power usage.

Changing the system timer to 1ms suddenly all this code that was only waking up every 15ms starts to wake up much more often, and the CPU usage goes up.

However from the users perspective the programs that have misused the timers can become more responsive, even the OS in the case of WinXP, so there is a trade off.

I have a small program that changes the system timer so you can experiment and test the power usage for yourself. There are also a number of links and some more background at http://www.lucashale.com/timer-resolution/

Lucas Hale
  • 51
  • 1
  • _does impact on power usage, mainly because lots of developers do not understand windows timers_? What's that? – Arno Jan 31 '14 at 11:23