timing


Nametiming JSON
Version 0.5.1 PyPI version JSON
download
home_pagehttps://github.com/mbdevpl/timing
SummarySimplify logging of timings of selected parts of an application.
upload_time2023-09-23 05:21:26
maintainerMateusz Bysiek
docs_urlNone
authorMateusz Bysiek
requires_python>=3.11
licenseApache License 2.0
keywords timing timer time measurement profiling reproducibility
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            .. role:: python(code)
    :language: python


======
timing
======

Simplify logging of timings of selected parts of an application.

.. image:: https://img.shields.io/pypi/v/timing.svg
    :target: https://pypi.org/project/timing
    :alt: package version from PyPI

.. image:: https://github.com/mbdevpl/timing/actions/workflows/python.yml/badge.svg?branch=main
    :target: https://github.com/mbdevpl/timing/actions
    :alt: build status from GitHub

.. image:: https://codecov.io/gh/mbdevpl/timing/branch/main/graph/badge.svg
    :target: https://codecov.io/gh/mbdevpl/timing
    :alt: test coverage from Codecov

.. image:: https://api.codacy.com/project/badge/Grade/5dba9ea9f47e4e86aeed6eddfce42640
    :target: https://app.codacy.com/gh/mbdevpl/timing
    :alt: grade from Codacy

.. image:: https://img.shields.io/github/license/mbdevpl/timing.svg
    :target: https://github.com/mbdevpl/timing/blob/v0.5.1/NOTICE
    :alt: license

.. contents::
    :backlinks: none


How to use
==========

Recommended initialization is as follows.

.. code:: python

    import timing

    _TIME = timing.get_timing_group(__name__)  # type: timing.TimingGroup


This follows the conventions of :python:`logging` module.

.. code:: python

    import logging

    _LOG = logging.getLogger(__name__)

Any name can be used instead of :python:`__name__`.
However, if names of format :python:`module.sub.sub_sub` are used, this will create a timing
hierarchy where each timing data is stored in its proper location and can be queried easier.

The resulting :python:`_TIME` object is used to create individual timers,
and will handle storing results in cache, which later can be used to obtain timing statistics.

You can obtain the timer object directly via :python:`start(name)` method.
You'll need to manually call :python:`stop()` in this case.

.. code:: python

   timer = _TIME.start('spam')  # type: timing.Timing
   spam()
   more_spam()
   timer.stop()


You can also obtain the timer object indirectly via :python:`measure(name)` context manager.
The context manager will take care of calling :python:`stop()` at the end.

.. code:: python

    with _TIME.measure('ham') as timer:  # type: timing.Timing
        ham()
        more_ham()


And if you want to time many repetitions of the same action (e.g. for statistical significance)
you can use :python:`measure_many(name[, samples][, threshold])` generator.

You can decide how many times you want to measure via :python:`samples` parameter
and how many seconds at most you want to spend on measurements via :python:`threshold` parameter

.. code:: python

    for timer in _TIME.measure_many('eggs', samples=1000):  # type: timing.Timing
        eggs()
        more_eggs()

    for timer in _TIME.measure_many('bacon', threshold=0.5):  # type: timing.Timing
        bacon()
        more_bacon()

    for timer in _TIME.measure_many('tomatoes', samples=500, threshold=0.5):  # type: timing.Timing
        tomatoes()
        more_tomatoes()


Also, you can use :python:`measure` and :python:`measure(name)` as decorator.
In this scenario you cannot access the timings directly, but the results will be stored
in the timing group object, as well as in the global cache unless you configure the timing
to not use the cache.

.. code:: python

    import timing

    _TIME = timing.get_timing_group(__name__)

    @_TIME.measure
    def recipe():
        ham()
        eggs()
        bacon()

    @_TIME.measure('the_best_recipe')
    def bad_recipe():
        spam()
        spam()
        spam()


Then, after calling each function the results can be accessed through :python:`summary` property.

.. code:: python

    recipe()
    bad_recipe()
    bad_recipe()

    assert _TIME.summary['recipe']['samples'] == 1
    assert _TIME.summary['the_best_recipe']['samples'] == 2


The :python:`summary` property is dynamically computed on first access. Subsequent accesses
will not recompute the values, so if you need to access the updated results,
call the :python:`summarize()` method.

.. code:: python

    recipe()
    assert _TIME.summary['recipe']['samples'] == 1

    bad_recipe()
    bad_recipe()
    assert _TIME.summary['the_best_recipe']['samples'] == 2  # will fail
    _TIME.summarize()
    assert _TIME.summary['the_best_recipe']['samples'] == 2  # ok


Further API and documentation are in development.


See these examples in action in `<examples.ipynb>`_ notebook.


Requirements
============

Python version 3.11 or later.

Python libraries as specified in `requirements.txt <https://github.com/mbdevpl/timing/blob/v0.5.1/requirements.txt>`_.

Building and running tests additionally requires packages listed in `<test_requirements.txt>`_.

Tested on Linux, macOS and Windows.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/mbdevpl/timing",
    "name": "timing",
    "maintainer": "Mateusz Bysiek",
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": "mateusz.bysiek@gmail.com",
    "keywords": "timing,timer,time measurement,profiling,reproducibility",
    "author": "Mateusz Bysiek",
    "author_email": "mateusz.bysiek@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/d5/5f/e6f5d568543e7372c12aea5913a99bf72862fab8f5355751980986fc9632/timing-0.5.1.tar.gz",
    "platform": null,
    "description": ".. role:: python(code)\n    :language: python\n\n\n======\ntiming\n======\n\nSimplify logging of timings of selected parts of an application.\n\n.. image:: https://img.shields.io/pypi/v/timing.svg\n    :target: https://pypi.org/project/timing\n    :alt: package version from PyPI\n\n.. image:: https://github.com/mbdevpl/timing/actions/workflows/python.yml/badge.svg?branch=main\n    :target: https://github.com/mbdevpl/timing/actions\n    :alt: build status from GitHub\n\n.. image:: https://codecov.io/gh/mbdevpl/timing/branch/main/graph/badge.svg\n    :target: https://codecov.io/gh/mbdevpl/timing\n    :alt: test coverage from Codecov\n\n.. image:: https://api.codacy.com/project/badge/Grade/5dba9ea9f47e4e86aeed6eddfce42640\n    :target: https://app.codacy.com/gh/mbdevpl/timing\n    :alt: grade from Codacy\n\n.. image:: https://img.shields.io/github/license/mbdevpl/timing.svg\n    :target: https://github.com/mbdevpl/timing/blob/v0.5.1/NOTICE\n    :alt: license\n\n.. contents::\n    :backlinks: none\n\n\nHow to use\n==========\n\nRecommended initialization is as follows.\n\n.. code:: python\n\n    import timing\n\n    _TIME = timing.get_timing_group(__name__)  # type: timing.TimingGroup\n\n\nThis follows the conventions of :python:`logging` module.\n\n.. code:: python\n\n    import logging\n\n    _LOG = logging.getLogger(__name__)\n\nAny name can be used instead of :python:`__name__`.\nHowever, if names of format :python:`module.sub.sub_sub` are used, this will create a timing\nhierarchy where each timing data is stored in its proper location and can be queried easier.\n\nThe resulting :python:`_TIME` object is used to create individual timers,\nand will handle storing results in cache, which later can be used to obtain timing statistics.\n\nYou can obtain the timer object directly via :python:`start(name)` method.\nYou'll need to manually call :python:`stop()` in this case.\n\n.. code:: python\n\n   timer = _TIME.start('spam')  # type: timing.Timing\n   spam()\n   more_spam()\n   timer.stop()\n\n\nYou can also obtain the timer object indirectly via :python:`measure(name)` context manager.\nThe context manager will take care of calling :python:`stop()` at the end.\n\n.. code:: python\n\n    with _TIME.measure('ham') as timer:  # type: timing.Timing\n        ham()\n        more_ham()\n\n\nAnd if you want to time many repetitions of the same action (e.g. for statistical significance)\nyou can use :python:`measure_many(name[, samples][, threshold])` generator.\n\nYou can decide how many times you want to measure via :python:`samples` parameter\nand how many seconds at most you want to spend on measurements via :python:`threshold` parameter\n\n.. code:: python\n\n    for timer in _TIME.measure_many('eggs', samples=1000):  # type: timing.Timing\n        eggs()\n        more_eggs()\n\n    for timer in _TIME.measure_many('bacon', threshold=0.5):  # type: timing.Timing\n        bacon()\n        more_bacon()\n\n    for timer in _TIME.measure_many('tomatoes', samples=500, threshold=0.5):  # type: timing.Timing\n        tomatoes()\n        more_tomatoes()\n\n\nAlso, you can use :python:`measure` and :python:`measure(name)` as decorator.\nIn this scenario you cannot access the timings directly, but the results will be stored\nin the timing group object, as well as in the global cache unless you configure the timing\nto not use the cache.\n\n.. code:: python\n\n    import timing\n\n    _TIME = timing.get_timing_group(__name__)\n\n    @_TIME.measure\n    def recipe():\n        ham()\n        eggs()\n        bacon()\n\n    @_TIME.measure('the_best_recipe')\n    def bad_recipe():\n        spam()\n        spam()\n        spam()\n\n\nThen, after calling each function the results can be accessed through :python:`summary` property.\n\n.. code:: python\n\n    recipe()\n    bad_recipe()\n    bad_recipe()\n\n    assert _TIME.summary['recipe']['samples'] == 1\n    assert _TIME.summary['the_best_recipe']['samples'] == 2\n\n\nThe :python:`summary` property is dynamically computed on first access. Subsequent accesses\nwill not recompute the values, so if you need to access the updated results,\ncall the :python:`summarize()` method.\n\n.. code:: python\n\n    recipe()\n    assert _TIME.summary['recipe']['samples'] == 1\n\n    bad_recipe()\n    bad_recipe()\n    assert _TIME.summary['the_best_recipe']['samples'] == 2  # will fail\n    _TIME.summarize()\n    assert _TIME.summary['the_best_recipe']['samples'] == 2  # ok\n\n\nFurther API and documentation are in development.\n\n\nSee these examples in action in `<examples.ipynb>`_ notebook.\n\n\nRequirements\n============\n\nPython version 3.11 or later.\n\nPython libraries as specified in `requirements.txt <https://github.com/mbdevpl/timing/blob/v0.5.1/requirements.txt>`_.\n\nBuilding and running tests additionally requires packages listed in `<test_requirements.txt>`_.\n\nTested on Linux, macOS and Windows.\n",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "Simplify logging of timings of selected parts of an application.",
    "version": "0.5.1",
    "project_urls": {
        "Homepage": "https://github.com/mbdevpl/timing"
    },
    "split_keywords": [
        "timing",
        "timer",
        "time measurement",
        "profiling",
        "reproducibility"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7a66f7341c8b8a33823fb64385c135dc78c424366e95b0faa88778f7a89e2531",
                "md5": "79e4e1a4940b3ad09094dd8454ae45b3",
                "sha256": "9dcdeaa6683a1f48f518f3fb9322e418cb9a6edd831a821e8fec991ada77de56"
            },
            "downloads": -1,
            "filename": "timing-0.5.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "79e4e1a4940b3ad09094dd8454ae45b3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 13619,
            "upload_time": "2023-09-23T05:21:24",
            "upload_time_iso_8601": "2023-09-23T05:21:24.339999Z",
            "url": "https://files.pythonhosted.org/packages/7a/66/f7341c8b8a33823fb64385c135dc78c424366e95b0faa88778f7a89e2531/timing-0.5.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d55fe6f5d568543e7372c12aea5913a99bf72862fab8f5355751980986fc9632",
                "md5": "98c5a2a6d239b1f7e171fdb108b0a616",
                "sha256": "cbf3b0708fa17a7130f281619663d3ed7626078e6c23bc29ebc2027cdc254937"
            },
            "downloads": -1,
            "filename": "timing-0.5.1.tar.gz",
            "has_sig": false,
            "md5_digest": "98c5a2a6d239b1f7e171fdb108b0a616",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 15541,
            "upload_time": "2023-09-23T05:21:26",
            "upload_time_iso_8601": "2023-09-23T05:21:26.238914Z",
            "url": "https://files.pythonhosted.org/packages/d5/5f/e6f5d568543e7372c12aea5913a99bf72862fab8f5355751980986fc9632/timing-0.5.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-23 05:21:26",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "mbdevpl",
    "github_project": "timing",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "timing"
}
        
Elapsed time: 0.12695s