Skip to content

Latest commit

 

History

History
60 lines (46 loc) · 1.41 KB

tuto_timer.md

File metadata and controls

60 lines (46 loc) · 1.41 KB
jupytext kernelspec
text_representation
extension format_name format_version jupytext_version
.md
myst
0.13
1.16.1
display_name language name
Python 3
python
python3

Timer

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)')