In your first test, you use QTimer, which treats a timer as a background task. QT is primarily focused on providing a flexible graphical interface.
In your second test, you have a print statement in a loop - there are a number of factors in the print that can cause a change in the time required to execute the instruction.
Take a look at the threading.Timer class for a better approach.
The documentation says:
This class represents an action that should only be started after a certain amount of time has passed - a timer. Timer is a subclass of Thread and by itself also serves as an example of creating custom threads.
Timers are started, like threads, by calling the start () method. The timer can be stopped (before it starts) by calling the cancel () method.
Note that he also says:
The interval that the timer will wait before its action may not be the same as the interval specified by the user.
In other words, it will not be perfect, but it will most likely be significantly better than what you see now.
If you are interested in MEASUREMENT time with higher accuracy, and not in planning tasks with better accuracy, consider using time.perf_counter() , which is available in Python 3.
source share