timer

Project description

Python code timer, support block wise and function wise

Installation

pip install timer

Usage

  1. import

    from timer import timer
    
  2. decorate without brackets

    @timer
    def func(): ...
    
  3. decorate with brackets

    @timer()
    def func(): ...
    
  4. decorate with name and time unit

    @timer('function name', 's')
    def func(): ...
    
  5. decorate with key word arguments

    @timer(name='function name', unit='s')
    def func(): ...
    
  6. 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

Download files

Download the file for your platform. If you’re not sure which to choose, learn more about installing packages.

Files for timer, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size

timer-0.0.3-py3-none-any.whl

(6.8 kB)

File type
Wheel
Python version

py3

Upload date

Sep 12, 2020

Hashes

View

Filename, size

timer-0.0.3.tar.gz

(2.6 kB)

File type
Source
Python version

None

Upload date

Sep 12, 2020

Hashes

View

Latest posts