tiner

Block-wise timer for Python


Keywords
python-library, timer-application
License
MIT
Install
pip install tiner==0.0.7

Documentation

tiner

Block-wise, thread-safety timer for loops

Install

pip install tiner

Usage

Works like a context manager

from tiner import tiner
from time import sleep

with tiner("see this block"):
  sleep(1)
# return the block running time
print(tiner.get('see this block'))

or as a python decorator

@tiner('see this function')
def f():
  #do something

Global mining and grouping

the timing is managed by tiner, not its instances:

# A.py
for _ in range(20):
  with tiner("t1"):
    #do something
...
# B.py
for _ in range(20):
  with tiner("t2"):
    #do something
...
# main.py
tiner.table()
#-------------------------
โ•’โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•โ•โ••
โ”‚ Block   โ”‚   Time(s) โ”‚   Hits โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ t1      โ”‚ 0.026127  โ”‚     20 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ t2      โ”‚ 0.0131467 โ”‚     10 โ”‚
โ•˜โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•โ•โ•›

tiner internally records the different locations for the same block name, display the additional infomation with tiner.table(verbose=True):

for _ in range(10):
  with tiner("test:loop"):
    sleep(duration)
  
tiner.table(verbose=True)
#-------------------------
test:loop
โ•’โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•โ•โ••
โ”‚ File                โ”‚   Line โ”‚   Time(s) โ”‚   Hits โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ tests/test_tiner.py โ”‚    107 โ”‚ 0.0128279 โ”‚     10 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ tests/test_tiner.py โ”‚    112 โ”‚ 0.0132992 โ”‚     10 โ”‚
โ•˜โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•โ•โ•›

Design for loops

from tiner import tiner
from time import sleep

for _ in range(10):
  #do something
  with tiner("see this loop"):
    sleep(0.1)
  #do something
  
# return the block running time over the loops
print(tiner.get('see this loop'))

Handle asynchronous programs

import os
from tiner import tiner

# tiner will call the synchronize function when the block is over
with tiner("loop", synchronize=torch.cuda.synchronize):
  # machine learning running
  
# return the block running time over the loops
print(tiner.get('loop'))

Easy to use

A timer should be clear and simple

tiner.get(BLOCK_NAME) # return a certain block running time so far
tiner.table([BLOCK1, ...]) # print some blocks' time on a formatted table
tiner.zero([BLOCK1, ...]) # empty some blocks' time
tiner.disable() # disable time logging
tiner.enable() # enable time logging

NOTE: tiner's timing is relatively precise. You should only use it for comparison of the timings of different blocks in one run, not for different runs of your program.