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 (in future) 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 and 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
Built Distribution
File details
Details for the file what2_time-0.3.1.tar.gz
.
File metadata
- Download URL: what2_time-0.3.1.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a9c7412d95948b3c7ee8eabe99d9bdff2ec6152ee99bb958eaadbb3661e9143 |
|
MD5 | ceea34594fd47c3a64f5742a6db7e3bf |
|
BLAKE2b-256 | ad3eb83f529b32920eff885c8530b688ea1aa461de70cf54db16a063cd733acf |
File details
Details for the file what2_time-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: what2_time-0.3.1-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 147b92defe3125dc287fd2fc4390ada3d6a6a00317071da9796504072d9231bf |
|
MD5 | 4e20d6bf526b602726e53241a9421f54 |
|
BLAKE2b-256 | fc678bdf4fbbaf412e4b1ecebab63aec7405979b6a5dd00b3bcc1aa53e791f2a |