Python Code Timer
Project description
Timer
Python code timer, support block wise and function wise
Installation
pip install timer
Usage
-
import
from timer import timer
-
decorate without brackets
@timer def func(): ...
-
decorate with brackets
@timer() def func(): ...
-
decorate with name and time unit
@timer('function name', 's') def func(): ...
-
decorate with key word arguments
@timer(name='function name', unit='s') def func(): ...
-
block wise
with timer(): ...
Sample Code
import logging
import time
from timer import timer
# timer would print nothing without this line or logging level is info or higher
logging.basicConfig(level=logging.DEBUG)
# explicit the timer's name and it's time unit
@timer('function:add', unit='s')
def add(a, b):
time.sleep(.1)
return a + b
# function name is timer's name for default
@timer
def sub(a, b):
time.sleep(.1)
return a - b
if __name__ == '__main__':
# 'timer' would be timer's name by default
with timer('time.sleep(2)'):
print(3)
time.sleep(2)
print(add(1, 1))
print(sub(2, 1))
Outputs
3
DEBUG:timer.time.sleep(2): 2.004 s
DEBUG:timer.function:add: 0.105 s
2
DEBUG:timer.sub: 102 ms
1
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
timer-0.0.3.tar.gz
(2.6 kB
view hashes)
Built Distribution
timer-0.0.3-py3-none-any.whl
(6.8 kB
view hashes)