Skip to main content

Python Code Timer

Project description

Timer

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 without object

    with timer():
        ...
    
  7. 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's logging level is logging.DEBUG
# so timer would print nothing if logging level is logging.INFO or higher
logging.basicConfig(level=logging.DEBUG)

# or you can change default timer's logging level
timer.set_level(logging.DEBUG)

# also 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

@Krzysztof S

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.3.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

timer-0.3.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file timer-0.3.0.tar.gz.

File metadata

  • Download URL: timer-0.3.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for timer-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9b11b71a48648f878dc4c29aa84387806d193ed9f71a39e84edb10f38ed6941f
MD5 bc4f936f269e0c4a06beb68a26943920
BLAKE2b-256 2f734f181969840995cfc3e160108ff91a9732c40578e1e2e0ea9a42a56bd543

See more details on using hashes here.

File details

Details for the file timer-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: timer-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for timer-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d97061be15de87d309697c3088a30af58712bd60c971536161fccc0e3d051f3
MD5 5f730b3647a5d9127809b51565011a7a
BLAKE2b-256 d3256b3dd2c9f899562718d6781d7ad8f1608ba760c4cb5c20730a7e9f0cc7f2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page