Name | bistiming JSON |
Version |
0.5.1.post0
JSON |
| download |
home_page | https://bistiming.readthedocs.io |
Summary | A logging-friendly stopwatch and profiling tool for Python. |
upload_time | 2024-11-07 16:11:38 |
maintainer | None |
docs_url | None |
author | Ian Lin |
requires_python | <4.0,>=3.7 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
BisTiming
=========
.. image:: https://github.com/ianlini/bistiming/actions/workflows/main.yml/badge.svg
:target: https://github.com/ianlini/bistiming/actions
.. image:: https://readthedocs.org/projects/pip/badge/
:target: https://bistiming.readthedocs.io/
.. image:: https://img.shields.io/pypi/v/bistiming.svg
:target: https://pypi.org/project/bistiming/
.. image:: https://img.shields.io/pypi/l/bistiming.svg
:target: https://github.com/ianlini/bistiming/blob/master/LICENSE
.. image:: https://img.shields.io/github/stars/ianlini/bistiming.svg?style=social
:target: https://github.com/ianlini/bistiming
A logging-friendly stopwatch and profiling tool for Python.
When we search the stopwatch or timing module for Python on the internet, we can find a
lot of code snippets, but none of them is powerful or convenient enough to do our daily
jobs.
BisTiming aims at implementing all the missing functions in those code snippets and
prevents us from reinventing the wheel.
It is very useful when we want to log something with some timing information or
optimize the performance of our code.
This package is tested with Python 3.8 to 3.14, but might also work in other
Python versions.
.. contents::
Installation
------------
.. code:: bash
pip install bistiming
Getting Started
---------------
BisTiming has a context manager interface that logs the running time of a code block
easily, and it also offers a low-level API to help time multiple segments or loops of
code easily.
See `examples <https://github.com/ianlini/bistiming/blob/master/examples/>`_
for all the useful examples.
Context Manager
+++++++++++++++
The simplest way to use BisTiming is by using the context manager ``Stopwatch``
and include the code we want to evaluate:
>>> from bistiming import Stopwatch
>>> from time import sleep
>>> with Stopwatch("Waiting"):
... print("do something")
... sleep(0.1)
... print("finished something")
...
...Waiting
do something
finished something
...Waiting done in 0:00:00.100330
We can use the parameter `logger` and `logging_level` to tell the stopwatch to output
using a logger:
>>> import logging
>>> logging.basicConfig(
... level=logging.DEBUG,
... format="[%(asctime)s] %(levelname)s: %(name)s: %(message)s")
>>> logger = logging.getLogger(__name__)
>>> with Stopwatch("Waiting", logger=logger, logging_level=logging.DEBUG):
... print("do something")
... sleep(0.1)
... print("finished something")
...
[2019-04-24 22:27:52,347] DEBUG: __main__: ...Waiting
do something
finished something
[2019-04-24 22:27:52,448] DEBUG: __main__: ...Waiting done in 0:00:00.100344
Another common use case is to evaluate the running time of a specific code segment
in a loop, we can initialize the stopwatch outside the loop, and reuse it in the loop:
>>> timer = Stopwatch("Waiting")
>>> for i in range(2):
... with timer:
... print("do something 1")
... sleep(0.1)
... print("finished something 1")
... print("do something 2")
... sleep(0.1)
... print("finished something 2")
...
...Waiting
do something 1
finished something 1
...Waiting done in 0:00:00.100468
do something 2
finished something 2
...Waiting
do something 1
finished something 1
...Waiting done in 0:00:00.100440
do something 2
finished something 2
>>> timer.split_elapsed_time
[datetime.timedelta(microseconds=100468),
datetime.timedelta(microseconds=100440)]
>>> timer.get_cumulative_elapsed_time()
datetime.timedelta(microseconds=200908)
Each item in ``split_elapsed_time`` is the running time of
the code segment in each iteration, and we can use
``get_cumulative_elapsed_time()``
to get the total running time of the code segment.
Low-level API
+++++++++++++
The low-level API is similar to a stopwatch in real life.
A simple use case using the low-level API is:
>>> from time import sleep
>>> from bistiming import Stopwatch
>>> timer = Stopwatch("Waiting").start()
...Waiting
>>> sleep(0.2) # do the first step of my program
>>> timer.split()
...Waiting done in 0:00:00.201457
>>> sleep(0.1) # do the second step of my program
>>> timer.split()
...Waiting done in 0:00:00.100982
The context manager
>>> with Stopwatch("Waiting"):
... sleep(0.1)
...Waiting
...Waiting done in 0:00:00.100330
is actually equivalent to the low-level API:
>>> timer = Stopwatch("Waiting").start()
...Waiting
>>> sleep(0.1)
>>> timer.pause()
>>> timer.split()
...Waiting done in 0:00:00.100330
Advance Profiling
+++++++++++++++++
``MultiStopwatch`` in this package contains multiple
``Stopwatch``, so we can use them to define each code segment
we want to evaluate and compare easily:
>>> from time import sleep
>>> from bistiming import MultiStopwatch
>>> timers = MultiStopwatch(2, verbose=False)
>>> for i in range(5):
... for i in range(2):
... with timers[0]:
... sleep(0.1)
... with timers[1]:
... sleep(0.1)
...
>>> print(timers.format_statistics())
╒═══════════════════════════╤══════════════╤════════════╤══════════════════╕
│ cumulative_elapsed_time │ percentage │ n_splits │ mean_per_split │
╞═══════════════════════════╪══════════════╪════════════╪══════════════════╡
│ 0:00:01.002417 │ 0.666377 │ 10 │ 0:00:00.100242 │
├───────────────────────────┼──────────────┼────────────┼──────────────────┤
│ 0:00:00.501861 │ 0.333623 │ 5 │ 0:00:00.100372 │
╘═══════════════════════════╧══════════════╧════════════╧══════════════════╛
Documentation
-------------
There are a lot more ways to use this package.
See the `documentation <https://bistiming.readthedocs.io>`_ for more information.
Raw data
{
"_id": null,
"home_page": "https://bistiming.readthedocs.io",
"name": "bistiming",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.7",
"maintainer_email": null,
"keywords": null,
"author": "Ian Lin",
"author_email": "you@example.com",
"download_url": "https://files.pythonhosted.org/packages/15/a5/01849d5880492c7ffc7ed4bf0420fc83223e1a8c1f588683cb860740244e/bistiming-0.5.1.post0.tar.gz",
"platform": null,
"description": "BisTiming\n=========\n.. image:: https://github.com/ianlini/bistiming/actions/workflows/main.yml/badge.svg\n :target: https://github.com/ianlini/bistiming/actions\n.. image:: https://readthedocs.org/projects/pip/badge/\n :target: https://bistiming.readthedocs.io/\n.. image:: https://img.shields.io/pypi/v/bistiming.svg\n :target: https://pypi.org/project/bistiming/\n.. image:: https://img.shields.io/pypi/l/bistiming.svg\n :target: https://github.com/ianlini/bistiming/blob/master/LICENSE\n.. image:: https://img.shields.io/github/stars/ianlini/bistiming.svg?style=social\n :target: https://github.com/ianlini/bistiming\n\nA logging-friendly stopwatch and profiling tool for Python.\n\nWhen we search the stopwatch or timing module for Python on the internet, we can find a\nlot of code snippets, but none of them is powerful or convenient enough to do our daily\njobs.\nBisTiming aims at implementing all the missing functions in those code snippets and\nprevents us from reinventing the wheel.\nIt is very useful when we want to log something with some timing information or\noptimize the performance of our code.\n\nThis package is tested with Python 3.8 to 3.14, but might also work in other\nPython versions.\n\n.. contents::\n\nInstallation\n------------\n.. code:: bash\n\n pip install bistiming\n\nGetting Started\n---------------\n\nBisTiming has a context manager interface that logs the running time of a code block\neasily, and it also offers a low-level API to help time multiple segments or loops of\ncode easily.\n\nSee `examples <https://github.com/ianlini/bistiming/blob/master/examples/>`_\nfor all the useful examples.\n\nContext Manager\n+++++++++++++++\n\nThe simplest way to use BisTiming is by using the context manager ``Stopwatch``\nand include the code we want to evaluate:\n\n>>> from bistiming import Stopwatch\n>>> from time import sleep\n>>> with Stopwatch(\"Waiting\"):\n... print(\"do something\")\n... sleep(0.1)\n... print(\"finished something\")\n...\n...Waiting\ndo something\nfinished something\n...Waiting done in 0:00:00.100330\n\nWe can use the parameter `logger` and `logging_level` to tell the stopwatch to output\nusing a logger:\n\n>>> import logging\n>>> logging.basicConfig(\n... level=logging.DEBUG,\n... format=\"[%(asctime)s] %(levelname)s: %(name)s: %(message)s\")\n>>> logger = logging.getLogger(__name__)\n>>> with Stopwatch(\"Waiting\", logger=logger, logging_level=logging.DEBUG):\n... print(\"do something\")\n... sleep(0.1)\n... print(\"finished something\")\n...\n[2019-04-24 22:27:52,347] DEBUG: __main__: ...Waiting\ndo something\nfinished something\n[2019-04-24 22:27:52,448] DEBUG: __main__: ...Waiting done in 0:00:00.100344\n\nAnother common use case is to evaluate the running time of a specific code segment\nin a loop, we can initialize the stopwatch outside the loop, and reuse it in the loop:\n\n>>> timer = Stopwatch(\"Waiting\")\n>>> for i in range(2):\n... with timer:\n... print(\"do something 1\")\n... sleep(0.1)\n... print(\"finished something 1\")\n... print(\"do something 2\")\n... sleep(0.1)\n... print(\"finished something 2\")\n...\n...Waiting\ndo something 1\nfinished something 1\n...Waiting done in 0:00:00.100468\ndo something 2\nfinished something 2\n...Waiting\ndo something 1\nfinished something 1\n...Waiting done in 0:00:00.100440\ndo something 2\nfinished something 2\n>>> timer.split_elapsed_time\n[datetime.timedelta(microseconds=100468),\n datetime.timedelta(microseconds=100440)]\n>>> timer.get_cumulative_elapsed_time()\ndatetime.timedelta(microseconds=200908)\n\nEach item in ``split_elapsed_time`` is the running time of\nthe code segment in each iteration, and we can use\n``get_cumulative_elapsed_time()``\nto get the total running time of the code segment.\n\nLow-level API\n+++++++++++++\nThe low-level API is similar to a stopwatch in real life.\nA simple use case using the low-level API is:\n\n>>> from time import sleep\n>>> from bistiming import Stopwatch\n>>> timer = Stopwatch(\"Waiting\").start()\n...Waiting\n>>> sleep(0.2) # do the first step of my program\n>>> timer.split()\n...Waiting done in 0:00:00.201457\n>>> sleep(0.1) # do the second step of my program\n>>> timer.split()\n...Waiting done in 0:00:00.100982\n\nThe context manager\n\n>>> with Stopwatch(\"Waiting\"):\n... sleep(0.1)\n...Waiting\n...Waiting done in 0:00:00.100330\n\nis actually equivalent to the low-level API:\n\n>>> timer = Stopwatch(\"Waiting\").start()\n...Waiting\n>>> sleep(0.1)\n>>> timer.pause()\n>>> timer.split()\n...Waiting done in 0:00:00.100330\n\nAdvance Profiling\n+++++++++++++++++\n``MultiStopwatch`` in this package contains multiple\n``Stopwatch``, so we can use them to define each code segment\nwe want to evaluate and compare easily:\n\n>>> from time import sleep\n>>> from bistiming import MultiStopwatch\n>>> timers = MultiStopwatch(2, verbose=False)\n>>> for i in range(5):\n... for i in range(2):\n... with timers[0]:\n... sleep(0.1)\n... with timers[1]:\n... sleep(0.1)\n...\n>>> print(timers.format_statistics())\n\u2552\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2564\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2564\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2564\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2555\n\u2502 cumulative_elapsed_time \u2502 percentage \u2502 n_splits \u2502 mean_per_split \u2502\n\u255e\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u256a\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u256a\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u256a\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2561\n\u2502 0:00:01.002417 \u2502 0.666377 \u2502 10 \u2502 0:00:00.100242 \u2502\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n\u2502 0:00:00.501861 \u2502 0.333623 \u2502 5 \u2502 0:00:00.100372 \u2502\n\u2558\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2567\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2567\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2567\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255b\n\nDocumentation\n-------------\nThere are a lot more ways to use this package.\nSee the `documentation <https://bistiming.readthedocs.io>`_ for more information.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A logging-friendly stopwatch and profiling tool for Python.",
"version": "0.5.1.post0",
"project_urls": {
"Homepage": "https://bistiming.readthedocs.io",
"Repository": "https://github.com/ianlini/bistiming"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "02eb0ee84f07112c3f460751b9def3824977e4bbf1485eb38fbedc7222151be5",
"md5": "454f3b3600a5b9477e06ad97facffb48",
"sha256": "27629c488613829ef4ddf08b717dbe1a6c4fb617b6aa54bb5fc4016f1ded8307"
},
"downloads": -1,
"filename": "bistiming-0.5.1.post0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "454f3b3600a5b9477e06ad97facffb48",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.7",
"size": 9922,
"upload_time": "2024-11-07T16:11:36",
"upload_time_iso_8601": "2024-11-07T16:11:36.291889Z",
"url": "https://files.pythonhosted.org/packages/02/eb/0ee84f07112c3f460751b9def3824977e4bbf1485eb38fbedc7222151be5/bistiming-0.5.1.post0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "15a501849d5880492c7ffc7ed4bf0420fc83223e1a8c1f588683cb860740244e",
"md5": "f4da2195c4a162a330dc0fa0ecad28d5",
"sha256": "252b404c2d3f128048338563174834206b94479c1d1612c069372e4473adc53b"
},
"downloads": -1,
"filename": "bistiming-0.5.1.post0.tar.gz",
"has_sig": false,
"md5_digest": "f4da2195c4a162a330dc0fa0ecad28d5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.7",
"size": 9393,
"upload_time": "2024-11-07T16:11:38",
"upload_time_iso_8601": "2024-11-07T16:11:38.881515Z",
"url": "https://files.pythonhosted.org/packages/15/a5/01849d5880492c7ffc7ed4bf0420fc83223e1a8c1f588683cb860740244e/bistiming-0.5.1.post0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-07 16:11:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ianlini",
"github_project": "bistiming",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "bistiming"
}