ticktock
Simple Python code metering library.
ticktock
is a minimalist library to profile Python code: it periodically displays timing of running code.
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] 50us count=1
Indicating that lines 3-5 took <50us to run.
You can use ticktock
arbitrarily deep inside you Python code and still get meaningful timing information.
When the timer 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] 50us count=1000
ticktock
acts as a context manager to track the timing of a chunk of code:
from ticktock import ticktock
with ticktock():
time.sleep(1)
Or as a decorator:
from ticktock import ticktock
@ticktock
def f():
time.sleep(1)
Checkout the documentation for a complete manual!