Simple Python code metering library.
Project description
ticktock
Simple Python code metering library.
ticktock
is a minimalist library to profile Python code, it displays timing of code snippets periodically.
Quickstart
First, install ticktock
:
pip install py-ticktock
Anywhere in your code you can use tick
to start a clock, and tock
to register the end of the snippet you want to time:
from ticktock import tick
clock = tick()
# do some work
clock.tock()
This will print
⏱️ [3-5] 1ms count=1
Indicating that lines 3-5 take <1ms to run.
If the timed snippet is called multiple times (for example within a loop), measured times will be aggregated and printed periodically (every 2 seconds by default).
As a result, the following code:
from ticktock import tick
for _ in range(1000):
clock = tick()
# do some work
clock.tock()
Will output:
⏱️ [4-6] 1ms count=1000
Advanced usage
Multiple Clocks
You can create multiple independent ticks, which will appear as two separate clocks:
for _ in range(1000):
clock = tick()
# do some work
time.sleep(1)
clock.tock()
clock = tick()
# do some other work
time.sleep(0.5)
clock.tock()
A single clock can have a multiple tocks
, which will be displayed as different lines
for k in range(1000):
t = tick()
# do some work
time.sleep(1)
if k % 2 == 1:
time.sleep(1)
t.tock()
else:
t.tock()
Context manager
It is also possible to use ticktock
as a context manager to track the timing of a chunk of code:
from ticktock import ticktock
with ticktock():
time.sleep(1)
Function decorator
Use the ticktock
decorator to track the timing of each call to a function:
from ticktock import ticktock
@ticktock
def f():
time.sleep(1)
f()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file py_ticktock-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: py_ticktock-0.0.5-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe820db293bae756dfe6f49ad88eb2060d1bc387d3a9cdff02dfb96f3dd170ed |
|
MD5 | c7b0942aa68ceaebf925222ba8f70149 |
|
BLAKE2b-256 | e05f2bf8163d1251050e8e9e6d6544b57a729054da00ce08c218394b382b5d88 |