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
# 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)') 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}')
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
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.1.2.tar.gz
(3.2 kB
view details)
Built Distribution
timer-0.1.2-py3-none-any.whl
(7.5 kB
view details)
File details
Details for the file timer-0.1.2.tar.gz
.
File metadata
- Download URL: timer-0.1.2.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.0 importlib_metadata/3.7.3 packaging/20.9 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba087c087daad7998892d6d31ab06ed831179b4d8e19b8898a21eb4e07d6ab73 |
|
MD5 | 5f016946a247ae76bd40a5fe4c8d8282 |
|
BLAKE2b-256 | 4961882c98724d01247fbf5dad2514baffb10bf5f60386b861a04f5d25532174 |
File details
Details for the file timer-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: timer-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.0 importlib_metadata/3.7.3 packaging/20.9 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6065d974cef5f4cad53108bf94ab251f9701b49b5a1e66a28122988406559ef1 |
|
MD5 | 124ce73fd8e459a191517886faca2ebd |
|
BLAKE2b-256 | 29b08cc11a2481ed93862e72c665040d30bd2920d813d818a7886f12298c20ca |