ticktock
Simple Python code metering library.
ticktock
is a minimalist library to view Python time performance of Python code.
First, install ticktock
:
pip install py-ticktock
Then, 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:
t = tick()
# do some work
t.tock()
Even if the code is called many times within a loop, measured times will only periodially (2 seconds by default):
import time
from ticktock import tick
for _ in range(1000):
t = tick()
# do some work
time.sleep(1)
t.tock()
You can create multiple independent ticks, which will appear as two separate clocks:
for _ in range(1000):
t = tick()
# do some work
time.sleep(1)
t.tock()
t = tick()
# do some other work
time.sleep(0.5)
t.tock()
Ticks can share a common starting point:
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()
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)
Or a decorator, to track the timing of each call to a function:
from ticktock import ticktock
@ticktock
def f():
time.sleep(1)
f()