Skip to main content

Reusable timing code that handles unit conversion, pretty printing time, total of repeat and re-entrant timers.

Project description

What 2 Time?

Reusable timing code that handles unit conversion, pretty printing time, total of repeat and re-entrant timers.

A timer can be creted and will default to printing total time on stopping:

>>> from what2_time import Timer
>>> t = Timer().start()
>>> t.stop()
Elapsed time: 1.8890 seconds

Or given a name for ease of inspection

>>> from what2_time import Timer
>>> t = Timer("FooTime").start()
>>> t.stop()
FooTime - Elapsed time: 1.4432 seconds

Alternatively, the output can be logged to any callable such as a logger:

>>> from what2_time import Timer
>>> import logging
>>> logger = logging.getLogger()
>>> logging.basicConfig(level=10)
>>> t = Timer(logger=logger.info).start()
>>> t.stop()
INFO:root:Elapsed time: 1.2577 seconds

Or instead set no logger handle logging yourself:

>>> from what2_time import Timer
>>> t = Timer(logger=None).start()
>>> elapsed = t.stop()
>>> print(elapsed)
1.587853118

A timer can also be used as a context manager:

>>> import time
>>> from what2_time import Timer
>>> with Timer("ContextTimer"):
...     time.sleep(1)
ContextTimer - Elapsed time: 1.0004 seconds

If you want to time the total time of something that is not performed in a single block, instead a MetaTimer can be used:

>>> import time
>>> from what2_time import MetaTimer
>>> with MetaTimer("MetaT"):
...     time.sleep(1)
...
>>> with MetaTimer("MetaT"):
...     time.sleep(1)
...
>>> print(MetaTimer.get_meta_duration("MetaT"))
2.00162006

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

what2_time-0.3.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

what2_time-0.3.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file what2_time-0.3.0.tar.gz.

File metadata

  • Download URL: what2_time-0.3.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.14.0a0 Linux/5.15.153.1-microsoft-standard-WSL2

File hashes

Hashes for what2_time-0.3.0.tar.gz
Algorithm Hash digest
SHA256 dbb2737a437fca1655846c9629c020800cece305ae0475eb2adc826ede73fd15
MD5 0718c77a1f46b0ce62add7a156e4b9d6
BLAKE2b-256 1ed2014fcb7259637c4f8e2146d6dafeae75d3da5141140db1c757c2e7f2addd

See more details on using hashes here.

File details

Details for the file what2_time-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: what2_time-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.14.0a0 Linux/5.15.153.1-microsoft-standard-WSL2

File hashes

Hashes for what2_time-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 17dce9135106fb1faa1db703e8f1a0ba8595384ebbba27886f457e41850db924
MD5 134300a24518bfc9a666fe57e4d677bf
BLAKE2b-256 9e19ebeecf5104b11848847d4bb5c5d1038e54faf1a742b4bb68e3690d02be1b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page