https://github.com/gusye1234/tiner
Block-wise, loop-cumulative timer for Python
https://github.com/gusye1234/tiner
python-library timer-application
Last synced: 11 months ago
JSON representation
Block-wise, loop-cumulative timer for Python
- Host: GitHub
- URL: https://github.com/gusye1234/tiner
- Owner: gusye1234
- License: mit
- Created: 2022-09-30T06:12:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-24T03:06:03.000Z (almost 3 years ago)
- Last Synced: 2025-06-08T05:45:08.491Z (12 months ago)
- Topics: python-library, timer-application
- Language: Python
- Homepage:
- Size: 34.2 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Install
```shell
pip install tiner
```
## Usage
### Works like a context manager
```python
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
```python
@tiner('see this function')
def f():
#do something
```
### Global mining and grouping
the timing is managed by `tiner`, not its instances:
```python
# 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 and will merge their duration at report. Display the additional infomation with `tiner.table(verbose=True)`:
```python
for _ in range(10):
with tiner("test:loop"):
sleep(duration)
...
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
```python
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
```python
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
```python
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
```