Timing issues when using sleep

Hi :wave:

I’m using the template-based code generation to develop a controller that runs at a fixed rate in c++.
I notice that using a sleep to schedule the function call to the controller, the computation time is affected by the rate, that is on the length of the sleep: the longer the sleep time, the longer the execution time (net of sleep time, of course).
This code reproduces the issue.

Thanks for any suggestions.

Hi Francesco,

I have a similar problem with Python using the function time.sleep().
I created a new post here.

Lorenzo

Hi Lorenzo, after some tests I’ve conducted, it seems that the issue is related to the operating system’s management of the CPU’s working frequencies. If you remove the sleep, the CPU keeps its working frequency at a steady state, and the execution time turns out to be lower. On Linux, you can force the CPU to maintain a high frequency by changing the governor settings to performance mode. I hope this can be of help.

Hi,
yes I reached the same conclusion about the CPU scheduling. Indeed, selecting the maximum performance mode on Ubuntu I obtain very fast computations also with the sleep.
Thanks!
Lorenzo