accurate-timed-loop


Nameaccurate-timed-loop JSON
Version 0.0.17 PyPI version JSON
download
home_pagehttps://bitbucket.org/arrizza-public/accurate-timed-loop/src/master
SummaryAccurate timed loop
upload_time2024-05-12 00:18:50
maintainerNone
docs_urlNone
authorJA
requires_pythonNone
licenseMIT
keywords accurate loop utility
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            website: <https://arrizza.com/python-accurate-timed-loop>

## Summary

This is a python module that provides 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 code

see sample.py for a full example

```python
import accurate_timed_loop

loop_delay = 0.250  # seconds
total_wait = 25.0  # seconds
for elapsed, start_time in accurate_timed_loop.accurate_wait(total_wait, loop_delay):
    # ... do task every 250 mS
    pass
```

## Scripts

* See [Quick Start](https://arrizza.com/user-guide-quick-start) for information on using scripts.
* See [xplat-utils submodule](https://arrizza.com/xplat-utils) for information on the submodule.

## Accuracy and Limitations

The 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.

```text
      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=0
```

This 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.

```python
import accurate_timed_loop

loop_delay = 0.250  # seconds
total_wait = 25.0  # seconds
adj = 0.009228  # macOS
for elapsed, start_time in accurate_timed_loop.accurate_wait(total_wait, loop_delay, fixed_adjustment=adj):
    # ... do task every 250 mS
    pass
```

Notes:

* 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 Windows

This report shows that std deviation is much better.

```text
      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=0
```

Limitations:

* there is NO guarantee that the average error will always be that low or that consistent
* the following runs were on a macOS

```text
# === 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.009369
```

* if 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 seconds
    * at 24.999 seconds instead of the expected 25.000 seconds

```text
      expected    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
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://bitbucket.org/arrizza-public/accurate-timed-loop/src/master",
    "name": "accurate-timed-loop",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "accurate loop, utility",
    "author": "JA",
    "author_email": "cppgent0@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/b0/0d/81e3fed964890a56826f640369e03d8563126c82194831f1eed9e7070b3f/accurate_timed_loop-0.0.17.tar.gz",
    "platform": null,
    "description": "website: <https://arrizza.com/python-accurate-timed-loop>\n\n## Summary\n\nThis is a python module that provides a way to have an acccurate timed loop.\n\nFor example if you need to do an activity every 250ms +/-10ms, this loop will do that.\n\n## Sample code\n\nsee sample.py for a full example\n\n```python\nimport accurate_timed_loop\n\nloop_delay = 0.250  # seconds\ntotal_wait = 25.0  # seconds\nfor elapsed, start_time in accurate_timed_loop.accurate_wait(total_wait, loop_delay):\n    # ... do task every 250 mS\n    pass\n```\n\n## Scripts\n\n* See [Quick Start](https://arrizza.com/user-guide-quick-start) for information on using scripts.\n* See [xplat-utils submodule](https://arrizza.com/xplat-utils) for information on the submodule.\n\n## Accuracy and Limitations\n\nThe sample.py does testing and shows that on Windows MSYS2 the std deviation error is roughly\n4mS in a 250mS loop. This means that 95% of loops will be +/-8 mS of the requested loop_delay.\n\n```text\n      expected    elapsed  diff1(ms)  actual(s)  diff2(ms)\n  1   0.000000   0.000000      0.000   0.000000      0.000\n  2   0.250000   0.257294      7.294   0.257294      7.294\n<snip>\n100  24.750000  24.764093     14.093  24.764093     14.093\n101  25.000000  25.015579     15.579  25.015579     15.579\n\n\nStats:\nloop count     : 101 loops\nError Range    : 0.000 to 24.406 mS\nError Stddev   :      5.009 mS\nError Average  :      8.863 mS\nRecommended adj: 0.012200\n     sample rc=0\n     doit overall rc=0\n```\n\nThis value is specific to Windows and to the PC that it is running on.\n\nTo make it more accurate for your PC and OS use the fixed_adjustment parameter.\nSet it so the minimum and maximum are roughly symmetrical around 0.\nThe Stdev and Average error values at that point should be miminal.\n\n```python\nimport accurate_timed_loop\n\nloop_delay = 0.250  # seconds\ntotal_wait = 25.0  # seconds\nadj = 0.009228  # macOS\nfor elapsed, start_time in accurate_timed_loop.accurate_wait(total_wait, loop_delay, fixed_adjustment=adj):\n    # ... do task every 250 mS\n    pass\n```\n\nNotes:\n\n* Re-run this several times, and tweak the fixed adjustment.\n* The sample.py reports a \"Recommended adj\" that usually results in better accuracy.\n* macOS and Ubuntu tend to be less variant than Windows\n\nThis report shows that std deviation is much better.\n\n```text\n      expected    elapsed  diff1(ms)  actual(s)  diff2(ms)\n  1   0.000000   0.000000      0.000   0.000000      0.000\n  2   0.250000   0.251537      1.537   0.251537      1.537\n<snip>\n101  25.000000  24.989502    -10.498  24.989502    -10.498\n102  25.250000  25.241386     -8.614  25.241386     -8.614\n\n\nStats:\nloop count     : 102 loops\nError Range    : -9.228 to 5.864 mS\nError Stddev   :      1.238 mS\nError Average  :      4.953 mS\nRecommended adj: 0.009228\n     sample rc=0\n     doit overall rc=0\n```\n\nLimitations:\n\n* there is NO guarantee that the average error will always be that low or that consistent\n* the following runs were on a macOS\n\n```text\n# === first run:\nStats:\nloop count     : 102 loops\nError Range    : -9.486 to 4.613 mS\nError Stddev   :      1.962 mS\nError Average  :      5.775 mS\nRecommended adj: 0.009486\n\n# === second run:\nStats:\nloop count     : 102 loops\nError Range    : -9.587 to 3.287 mS\nError Stddev   :      2.163 mS\nError Average  :      6.745 mS\nRecommended adj: 0.009587\n\n# === third run:\nStats:\nloop count     : 102 loops\nError Range    : -9.472 to 6.782 mS\nError Stddev   :      1.546 mS\nError Average  :      5.597 mS\nRecommended adj: 0.009472\n\n# === fourth run:\nStats:\nloop count     : 101 loops\nError Range    : -9.518 to 10.365 mS\nError Stddev   :      1.865 mS\nError Average  :      5.410 mS\nRecommended adj: 0.009518\n\n# === fifth run:\nStats:\nloop count     : 101 loops\nError Range    : -9.369 to 13.726 mS\nError Stddev   :      2.196 mS\nError Average  :      5.614 mS\nRecommended adj: 0.009369\n```\n\n* if you use the adj parameter the incoming \"elapsed\" parameter will not be after your expected delay.\n  For example these two came in:\n    * at 24.749 seconds instead of the expected 24.750 seconds\n    * at 24.999 seconds instead of the expected 25.000 seconds\n\n```text\n      expected    elapsed  diff1(ms)  actual(s)  diff2(ms)\n100  24.750000  24.749573     -0.427  24.749573     -0.427\n101  25.000000  24.999601     -0.399  24.999601     -0.399\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Accurate timed loop",
    "version": "0.0.17",
    "project_urls": {
        "Download": "https://bitbucket.org/arrizza-public/accurate-timed-loop/get/master.zip",
        "Homepage": "https://bitbucket.org/arrizza-public/accurate-timed-loop/src/master"
    },
    "split_keywords": [
        "accurate loop",
        " utility"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b00d81e3fed964890a56826f640369e03d8563126c82194831f1eed9e7070b3f",
                "md5": "3b16623597bd0a7fb87c6d1f702431bd",
                "sha256": "f5e50e497325bc92a03c1bbc18acdc0a3a18d603d7c7a5ad36d982d6b145b63e"
            },
            "downloads": -1,
            "filename": "accurate_timed_loop-0.0.17.tar.gz",
            "has_sig": false,
            "md5_digest": "3b16623597bd0a7fb87c6d1f702431bd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 31356,
            "upload_time": "2024-05-12T00:18:50",
            "upload_time_iso_8601": "2024-05-12T00:18:50.030145Z",
            "url": "https://files.pythonhosted.org/packages/b0/0d/81e3fed964890a56826f640369e03d8563126c82194831f1eed9e7070b3f/accurate_timed_loop-0.0.17.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-12 00:18:50",
    "github": false,
    "gitlab": false,
    "bitbucket": true,
    "codeberg": false,
    "bitbucket_user": "arrizza-public",
    "bitbucket_project": "accurate-timed-loop",
    "lcname": "accurate-timed-loop"
}
        
JA
Elapsed time: 0.25907s