Package: accurate-timed-loop
Accurate Timed LoopThis is a python module that provide a way to have an acccurate timed loop.For example if you need to do an activity every 250ms +/-10ms, this loop will do that.Sample codesee sample.py for a full exampleimportaccurate_timed_looploop_delay=0.250# secondstotal_wait=25.0# secondsforelapsed,start_timeinaccurate_timed_loop.accurate_wait(total_wait,loop_delay):# ... do task every 250 mSpassAccuracy and LimitationsThe sample.py does testing and shows that on Windows MSYS2 the std deviation error is roughly
4mS in a 250mS loop. This means that 95% of loops will be +/-8 mS of the requested loop_delay.expected    elapsed  diff1(ms)  actual(s)  diff2(ms)
  1   0.000000   0.000000      0.000   0.000000      0.000
  2   0.250000   0.257294      7.294   0.257294      7.294
<snip>
100  24.750000  24.764093     14.093  24.764093     14.093
101  25.000000  25.015579     15.579  25.015579     15.579


Stats:
loop count     : 101 loops
Error Range    : 0.000 to 24.406 mS
Error Stddev   :      5.009 mS
Error Average  :      8.863 mS
Recommended adj: 0.012200
     sample rc=0
     doit overall rc=0This value is specific to Windows and to the PC that it is running on.To make it more accurate for your PC and OS use the fixed_adjustment parameter.
Set it so the minimum and maximum are roughly symmetrical around 0.
The Stdev and Average error values at that point should be miminal.importaccurate_timed_looploop_delay=0.250# secondstotal_wait=25.0# secondsadj=0.009228# macosforelapsed,start_timeinaccurate_timed_loop.accurate_wait(total_wait,loop_delay,fixed_adjustment=adj):# ... do task every 250 mSpassNotes:Re-run this several times, and tweak the fixed adjustment.The sample.py reports a "Recommended adj" that usually results in better accuracy.Macos and Ubuntu tend to be less variant than WindowsThis report shows that std deviation is much better.expected    elapsed  diff1(ms)  actual(s)  diff2(ms)
  1   0.000000   0.000000      0.000   0.000000      0.000
  2   0.250000   0.251537      1.537   0.251537      1.537
<snip>
101  25.000000  24.989502    -10.498  24.989502    -10.498
102  25.250000  25.241386     -8.614  25.241386     -8.614


Stats:
loop count     : 102 loops
Error Range    : -9.228 to 5.864 mS
Error Stddev   :      1.238 mS
Error Average  :      4.953 mS
Recommended adj: 0.009228
     sample rc=0
     doit overall rc=0Limitations:there is NO guarantee that the average error will always be that low or that consistentthe following runs were on a Macos# === first run:
Stats:
loop count     : 102 loops
Error Range    : -9.486 to 4.613 mS
Error Stddev   :      1.962 mS
Error Average  :      5.775 mS
Recommended adj: 0.009486

# === second run:
Stats:
loop count     : 102 loops
Error Range    : -9.587 to 3.287 mS
Error Stddev   :      2.163 mS
Error Average  :      6.745 mS
Recommended adj: 0.009587

# === third run:
Stats:
loop count     : 102 loops
Error Range    : -9.472 to 6.782 mS
Error Stddev   :      1.546 mS
Error Average  :      5.597 mS
Recommended adj: 0.009472

# === fourth run:
Stats:
loop count     : 101 loops
Error Range    : -9.518 to 10.365 mS
Error Stddev   :      1.865 mS
Error Average  :      5.410 mS
Recommended adj: 0.009518

# === fifth run:
Stats:
loop count     : 101 loops
Error Range    : -9.369 to 13.726 mS
Error Stddev   :      2.196 mS
Error Average  :      5.614 mS
Recommended adj: 0.009369if you use the adj parameter the incoming "elapsed" parameter will not be after your expected delay.
For example these two came in:at 24.749 seconds instead of the expected 24.750 secondsat 24.999 seconds instead of the expected 25.000 secondsexpected    elapsed  diff1(ms)  actual(s)  diff2(ms)
100  24.750000  24.749573     -0.427  24.749573     -0.427
101  25.000000  24.999601     -0.399  24.999601     -0.399