Timing Python code made easy
Project description
timer
timer
is a library to time your Python code.
Installation
pip install timer4 # not timer
Usage
timer
useswith
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, callreport
.
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.0.2.tar.gz
(3.6 kB
view details)
Built Distribution
File details
Details for the file timer4-1.0.2.tar.gz
.
File metadata
- Download URL: timer4-1.0.2.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74ef56c6d9bc522b1ecf21516c912b28abcba7d8e52f25cefa3afbf6c1d6a328 |
|
MD5 | fc0cdd3c630adb111bec226316ca2ccb |
|
BLAKE2b-256 | 2a6cec6d51c47398fb3305fb06815a45fad835317220ac8425c775fe6c46dd7f |
File details
Details for the file timer4-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: timer4-1.0.2-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50a2c34e8adb3d8d1ef9fa0ac2ac65edbef26f3feb7e320d18eca574f84e9e83 |
|
MD5 | dbbf9fb5a15888b9728f75be3209429e |
|
BLAKE2b-256 | 310239faa09fbcf59487942da2937b774b8731efbf24a87cef88a4e15604c7a2 |