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 without object
with timer(): ...
-
block wise with object
with timer() as t: ... print(t.elapse)
Sample Code
import logging
import time
from timer import timer, get_timer
# default timer using logging level with logging.DEBUG
# so timer would print nothing if logging level is logging.INFO or higher
logging.basicConfig(level=logging.DEBUG)
# you can get a timer with custom logging level with get_timer(level)
warning_timer = get_timer(logging.WARNING)
# 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)') as t:
print(3)
time.sleep(1)
print(f'after time.sleep(1) once, t.elapse = {t.elapse}')
time.sleep(1)
print(f'after time.sleep(1) twice, t.elapse = {t.elapse}')
print(f'after with, t.elapse = {t.elapse}')
with warning_timer('test'):
pass
print(add(1, 1))
print(sub(2, 1))
Outputs
3
after time.sleep(1) once, t.elapse = 1.003798776
after time.sleep(1) twice, t.elapse = 2.0052743459999998
DEBUG:timer.time.sleep(2): 2.006 s
after with, t.elapse = 2.005628447
WARNING:timer.test:start
WARNING:timer.test:cost 0 ms
DEBUG:timer.function:add: 0.105 s
2
DEBUG:timer.sub: 102 ms
1
Special Thanks
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.2.0.tar.gz
(7.5 kB
view details)
Built Distribution
timer-0.2.0-py3-none-any.whl
(7.8 kB
view details)
File details
Details for the file timer-0.2.0.tar.gz
.
File metadata
- Download URL: timer-0.2.0.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35de0b0a1f1b02335ce4cda4ebde6069316d8ed185f9ce42e6110584ab8bfc70 |
|
MD5 | bcee0cfa84fbc1409a1d3bb760f84e15 |
|
BLAKE2b-256 | ae9fdc3fc1c6c4d24bc70c5099feee5e7f9675f9a5e6c3b0a0d4a8d7c527b76b |
File details
Details for the file timer-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: timer-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff4d4d9dae39eee5e6d3d94492578b5b90561048c7ecdc636bac20baa1d6390b |
|
MD5 | 28bf7cbd5fbf3bfab639345b06405b7c |
|
BLAKE2b-256 | af48cd504e9bfe7dd8044c977c3b60a7af9ee2883f47c13e612a9ecd91bb8cd9 |