psychopy.org | Reference | Downloads | Github

Non-slip timing: core.wait vs while loop

Hi everyone,

I want to implement non-slip timing in my script for an fMRI experiment and currently make use of a while loop that repeats as long as a certain duration (as measured by a global clock) is not reached yet:

clock = core.Clock()

for vol in range(1, 10):
    # experiment here
    while clock.getTime() < vol:
        pass

Now this gives me very accurate non-slip timing for the case of 1s per volume. However, I saw in the code for the SyncGenerator class (psychopy.hardware.emulator) the following code logic:

hogCPU = 0.035

for vol in range(1, 10):
    core.wait(1.0 - hogCPU, hogCPUperiod=0)
    while clock.getTime() < vol:
        pass

Here, the core.wait command is executed first before entering the while loop. Does this have any advantage over using the while loop only? The documentation of core.wait mentions that system resources benefit from the wait function call but why is that so?