Skip to main content

Elapsed timer and utilities

Project description

elapsedtimer

Python elapsed time utilities.

The main interface to this package is an ElapsedTimer class. This class will use the highest resolution timer available to Python depending on the OS, either time.time() or time.clock(). Its purpose is easily to measure and print the duration of a task, and is normally meant to be used as a context manager.

Basic example:

>>> with ElapsedTimer('say hello'):
...     print 'hi there!'
hi there!
13.113 µs: say hello

ElapsedTimer

The constructor for ElapsedTimer takes an optional string describing the operation being performed. It also optionally accepts a file object to change where the resulting duration message will be printed. The output file defaults to sys.stdout.

The constructor can also take a logger instance and log level via the optional logger and loglevel keyword parameters. If a logger is provided, it takes precedence over a file object and the duration message will be output via the logger. The log level defaults to DEBUG.

You can control an ElapsedTimer instance directly instead of using it as a context manager. It has start() and stop() methods. The stop() method will not print the duration for you like exiting a context manager instance does.

There is an elapsed property that returns the elapsed time since start() was called or the context manager entered. A timedelta property is also available that returns the elapsed time as a datetime.timedelta object instead of a float, though note that this class this only has microsecond resolution.

There is a module-level enable variable that acts as a global enable switch for all printing of results by ElapsedTimer. It defaults to True.

Timeout

Another class in the module is Timeout. It adds a few methods to make it easy to check for timeouts. You can use this class as a context manager. The constructor takes the same parameters as for ElapsedTimer, except for a new first param of the timeout in seconds.

There are two methods to check the timeout, check() and check_and_raise(). The former compares the elapsed time against the timeout and returned True if a timeout occurred. The latter will raise TimeoutError if a timeout happens. You can use the timed_out property to as another way to check, equivalent to calling check().

License

This package is licensed under the BSD three-clause license. See the LICENSE file for details.

Copyright © 2014-2022 Chris Reed.

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

elapsedtimer-1.0.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

elapsedtimer-1.0.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file elapsedtimer-1.0.0.tar.gz.

File metadata

  • Download URL: elapsedtimer-1.0.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.3

File hashes

Hashes for elapsedtimer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7f41d31c383232b8a64e55bb5dc8e440a094ba9f0f82eb12461410afe454693c
MD5 22ca2ac76a9b06b218589538f6cf91a0
BLAKE2b-256 ab46aae52ed9cfb26a2b8325d116ee66631c0b6880171ba16a125960f4f95cb0

See more details on using hashes here.

File details

Details for the file elapsedtimer-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: elapsedtimer-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.3

File hashes

Hashes for elapsedtimer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f577b94923b3c3867b7e76d751b066a73d14f43e6c01351ecfab082470f01bda
MD5 aa635b5a8ef650cb9d5563702d82d86a
BLAKE2b-256 c6adfda1e287d2c6de0b9e05f5b4f5a3045653249e1422689e0a65c2c750dadf

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