How much resource heavy are the timers?

144 views Asked by At

Suppose a timer is fired every 1 second to execute some task, which is not CPU intensive.
What is the resource heaviness of a timer itself?

Related to C# perspective: How resource heavy is a Timer?
[Note: Have tagged as well, if someone would want to answer from its signal/slot perspective.]

1

There are 1 answers

0
Marcus Müller On

This depends on your hardware, your operating system and the library you use to implement the timer.

But generally on application processors, a timer costs nothing computationally but an entry in a wait list, and a thread that's blocked until a hardware timer fires. That does get expensive if you have much more timers than your hardware can directly manage, so that you operating system has to maintain a queue of timers to prepare after the last one fired. It might, depending on operating system design, also necessitate a context switch, if the handling of the hardware timer interrupt happens in kernel space. In context of the signal/slot architecture of QT, there might be another layer of mutexes for inter-thread communication involved, so depending on whether these are userland of context-switching mutex implementations, that can infer another context switch in cost.

That all sounds scary, and it would be, would you have 100000 timers per second. You have one. That is incredibly little. Your will not be able to even measure this amount of overhead, especially in a context with GUI.