Python sleep() affects the computation time

Hi,

I am using acados Python interface to implement an MPC controller for aerial robotics.
My objective is to have the control running at a fixed frequency (100 Hz). I discussed a possible approach using the Python function time.sleep() here.
The time results are these (excluding sleep time of course)
Figure_1
where the red time is the total computation time (acados+my operations).

I tried also to use threading timers from the threading library but it seems that at high frequency the overhead introduced affects too much the performances, so I discarded that possibility.

I have another version of the same problem where the control does not have a fixed frequency, but it goes “as fast as it can” (without sleep) and samples the reference accordingly.
This version has much lower execution time, with average around 1.5 ms
Figure_2
and it also seem more regular.

Even my operations (e.g. rotate vectors, transform output) require much less time even if the code executed is the same.
Does someone has an explanation for that?

Another user had a similar problem here, but no solutions were proposed.

Lorenzo