jupytext | kernelspec | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
This notebook demonstrates how the classes :class:`fluiddyn.util.timer.Timer` and :class:`fluiddyn.util.timer.TimerIrregular` can be used to loop with controled times.
We import the 2 classes:
from fluiddyn.util.timer import Timer, TimerIrregular
Let's first use the regular timer:
timer = Timer(0.02)
for i in range(10):
print(f'before tick {i}... ', end='')
timer.wait_tick()
print("It's time to tick", i)
Ok. This is the simplest way to use it. But we don't see when the timer ticked. Let's print these times:
timer = Timer(0.02)
for i in range(10):
print(f'before tick {i}... ', end='')
t_tick = timer.wait_tick()
print("It's time to tick", i, f'(t = {t_tick:.4f} s)')
Now, we see that it's ticking regularly... and it seems with a quite good accuracy for many needs.
But what can we do if we need irregular steps between the ticks?
times = [0, 0.05, 0.07, 0.1]
timer = TimerIrregular(times)
for i in range(len(times)-1):
print(f'before tick {i}... ', end='')
t_tick = timer.wait_tick()
print("It's time to tick", i, f'(t = {t_tick:.4f} s)')