Skip to main content

measure/limit the function execution time, cross-platform

Project description


PyPI Build Status Coverage Status CodeFactor PyPI - Python Version platform

measure / limit the function execution time, cross-platform


pip install timework


import timework as tw


A decorator measuring the execution time.

timework.TimeError contains three parts:

  • TimeError.message string, <decorated function name>: <time used> seconds used
  • TimeError.result return values of the function being decorated
  • TimeError.detail float, time used

Notice: In @timer decorator, timeout is used to raise a Error after the decorated function finishes, but fails to finish within timeout seconds. If you want to terminate the function with a timeout limit, please use @limit.

import logging

def timer_demo_a():
    i = 0
    while i < 2 ** 23:
        i += 1
    return i

@tw.timer(print, detail=True)
def timer_demo_b():
    i = 0
    while i < 2 ** 24:
        i += 1
    return i

def timer_demo_c():
    i = 0
    while i < 2 ** 25:
        i += 1
    return i
a = timer_demo_a()
b = timer_demo_b()

    c = timer_demo_c()
except tw.TimeError as e:
    print('error:', e.message)
    c = e.result

print(a, b, c)
WARNING:root:timer_demo_a: 0.496672 seconds used
START:  Tue Feb 18 15:06:45 2020
FINISH: Tue Feb 18 15:06:46 2020
timer_demo_b: 0.989352 seconds used
error: timer_demo_c: 1.9817 seconds used
8388608 16777216 33554432


A decorator limiting the execution time.

timework.TimeError only contains:

  • TimeError.message string, <decorated function name>: <timeout> seconds exceeded
  • TimeError.result None, unused
  • TimeError.detail None, unused
def limit_demo(m):
    i = 0
    while i < 2 ** m:
        i += 1
    return i
    s = limit_demo(4)
except tw.TimeError as e:
    print('result:', s)

    s = limit_demo(30)
except tw.TimeError as e:
    print('result:', s)
result: 16
limit_demo: 3 seconds exceeded

You can read docstrings for more details.


MIT License © bugstop

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

timework-0.2.10.tar.gz (5.2 kB view hashes)

Uploaded source

Built Distribution

timework-0.2.10-py3-none-any.whl (5.8 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page