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

    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 details)

Uploaded Source

Built Distribution

timer-0.0.3-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: timer-0.0.3.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for timer-0.0.3.tar.gz
Algorithm Hash digest
SHA256 70fff81798577ca0d0429e996674915a144b4c393916098ae86bbc7f0b9f1444
MD5 ff758bc1655d0f035176f244d9d854e2
BLAKE2b-256 bab3b4f671f4637e88b9dd47a8ac6813cfd57e17c41b16ba29d1dc1053ea915b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: timer-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for timer-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f73628a8e8de6d85c2d37697c06b39282d34b8a2a370cd871c084ee26a2fe4ab
MD5 92c4a025b09332cb9e19285e2e739761
BLAKE2b-256 b84ce04e8a421b3fb56f68bebd9f03911aedc4bb19bdf33672c9572d122d546d

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