Skip to main content

Timing Python code made easy

Project description

timer PyPI Python

timer is a library to time your Python code.

Installation

pip install timer4  # not timer

Usage

  • timer uses with statement to watch how long your code running:
import time
from timer import Timer


with Timer().watch_and_report(msg='test'):
    # running code that do lots of computation
    time.sleep(1.0)

# when the code reach this part, it will output the message and the time it tooks.
# for example:
#     test: 10.291 seconds
  • If you don't want to report the result immediately, use the watch method instead. Whenever you've done, call report.
import time
from timer import Timer

# you can either create a timer variable first, or use Timer.get_instance()
# that will return a singleton variable.

total = 0
for item in range(7):
    # only measure the part that we want
    with Timer.get_instance().watch("sum of square"):
        total += item ** 2
        time.sleep(0.2)

    # doing other things that we don't want to measure
    time.sleep(0.8)

Timer.get_instance().report()
  • You can also use different way to print the message, such as using logging by passing a printing function to the report method: report(print_fn=logger.info)

  • You can also choose to append the result to a file report(append_to_file='/tmp/runtime.csv'). This is useful if you want to measure runtime of your method and put it to a file to plot it later.

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

timer4-1.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

timer4-1.1.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file timer4-1.1.0.tar.gz.

File metadata

  • Download URL: timer4-1.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for timer4-1.1.0.tar.gz
Algorithm Hash digest
SHA256 4a93c16a3fec06ac9a3afce92a28ceb7dff514829e1ca6d8b4b3f8287b4b98ba
MD5 33933e7410b9b99f205744997c2eed9b
BLAKE2b-256 3c22ee963fdb5e0e69c7547cfc0e8d66810b0266d025ce82d3395d4362e65b3c

See more details on using hashes here.

File details

Details for the file timer4-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: timer4-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for timer4-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 23d635ab721c04cc36b0a38333993bf057ebcb8067dd1b02c53d1c9b842f8ec8
MD5 71fedd6e7eb388bcafcad637ace003e1
BLAKE2b-256 1ab0cdd988729dcb76839b36aa970746361ce3e8a5e78ce552a2ffd20d9a05d3

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