reiz.time¶
A clock based on LSL local_clock
-
class
Clock
[source]¶ allows to keep track of time
reset the clock with
reset()
, and get the time since this reset withnow()
. Measure intermediate times withtick()
, which return time since last call oftick()
, oreach call of
tick()
updates the cumulative time since the last call ofreset()
. If you don’t want to add the time measured, usepause()
-
reset
()[source]¶ reset the clock
resets the counter keeping track of the cumulative time spend since instantiaion or the last call of
reset()
-
sleep
(duration)[source]¶ Sleep for duration in seconds
blocking is slightly more accurate than non-blocking sleep, e.g. as available with
time.sleep()
from the stdlib. There is one disadvantage of this kind of busy sleep: it can cause slight oversleeping, as there is an overhead of the function being called and returning, which is not accounted for. Seesleep_debiased()
for an alternative sleep with asymptotic minimisation of the error.- Parameters
duration (float) – how many seconds to sleep blocking
- Returns
duration – the time in seconds spent sleeping
- Return type
float
-
sleep_debiased
(duration)[source]¶ Sleep for duration in seconds with attempts for debiasing`
sometimes, you execute some other commands, and these commands have a variable runtime. If we would naively sleep everytime for n seconds afterwards, we would inherit this jitter. By using
tick()
before these commands, andsleep_debiased()
after these commands, we can normalize the runtime to a fixed period (as long as the sleep duration is longer than the runtime of the commands).Additionally, this function keeps track of any oversleeping or undersleeping, and will minimize the temporal error asymptotically
- Returns
duration – the time in seconds spent sleeping since the last call of
tick()
orsleep_debiased()
.- Return type
float
Example
This example shows how we can regularize the time spent in each cycle to 200ms in spite of there being an element of random runtime
import time import random from reiz.time import Clock clock = Clock() t = 0. msg = "{0:3.5f}, {1:3.5f}, {2:3.5f}, slept for {3:3.5f}s" for i in range(1, 11): time.sleep(random.random()/10) dt = clock.sleep_debiased(0.2) t += dt print(msg.format(i*0.2, clock.now(), t, dt))
-