Skip to main content

Various tools to easily measure elapsed time in python (with function wrapper, context manager, or decorator).

Project description

Time measure: an easy way to measure elapsed time in python

Features

  • Decorator for measuring function execution time
  • Context manager for measuring code block execution time
  • Function wrapper for measuring function calls
  • Standalone function for one-off time measurements

Installation

pip install time-measure

Usage

Standalone Function

Use measure_time() for one-off time measurements:

from time_measure import measure_time

def my_function(n):
    return sum(range(n))

measure_time(my_function, 10**7)

Decorator

Use the @time_measure_decorator() to measure function execution time:

from time_measure import time_measure_decorator

@time_measure_decorator()
def my_function(n):
    return sum(range(n))

my_function(10**7)
my_function(10**8)
my_function.print_stats()

Context manager

Function-based

Use time_measure_context() to measure execution time of a code block:

from time_measure import time_measure_context

with time_measure_context("my_function (time_measure_context)"):
    # Your code here
    sum(range(10**7))

Class-based

Use TimeMeasureContextManager for more control and multiple measurements:

from time_measure import TimeMeasureContextManager

time_measure_context = TimeMeasureContextManager("my_function (TimeMeasureContextManager)")

with time_measure_context():
    sum(range(10**7))

with time_measure_context():
    sum(range(10**8))
    sum(range(10**7))

time_measure_context.print_stats()

Function wrapper

Function-based

Wrap a function to measure its execution time:

from time_measure import time_measure_decorator

def my_function(n):
    return sum(range(n))

wrapped_function = time_measure_decorator(my_function)
wrapped_function(10**7)
wrapped_function(10**8)
wrapped_function.print_stats()

Class-based

Use TimeMeasureWrapper for more control:

from time_measure import TimeMeasureWrapper

def my_function(n):
    return sum(range(n))

time_measure_wrapper = TimeMeasureWrapper()
wrapped_function = time_measure_wrapper(my_function)
wrapped_function(10**7)
wrapped_function(10**8)
time_measure_wrapper.print_stats(my_function.__name__)

Output

The tool provides detailed timing information, including:

  • Execution time for each call
  • Average execution time
  • Total execution time
  • Number of calls

Example output at runtime:

[INFO] my_function (call 1): 0.088180 seconds (avg: 0.088180 seconds, total: 0.088180 seconds)
[INFO] my_function (call 2): 0.826875 seconds (avg: 0.457527 seconds, total: 0.915055 seconds)

Example output from a print_stats() call:

[INFO] my_function stats:
  Calls: 2
  Total time: 0.915055 seconds
  Average time: 0.457527 seconds

Contributing

If you encounter any issues or have suggestions for improvement, please open an issue in the repository or create a Pull Request.

License

This project is licensed under the MIT License.

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

time-measure-0.1.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

time_measure-0.1.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file time-measure-0.1.1.tar.gz.

File metadata

  • Download URL: time-measure-0.1.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.11

File hashes

Hashes for time-measure-0.1.1.tar.gz
Algorithm Hash digest
SHA256 86ff2a4a40585b69f26957c1e0c2d2aa501730e09757c38f8b623552c54798e9
MD5 27cd629859ae5455646a890c6d8eb7cf
BLAKE2b-256 523fd1eca5293c8926ca890a778918e0942eaef1431bf5448c0eb20d2d62bcc3

See more details on using hashes here.

File details

Details for the file time_measure-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: time_measure-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.11

File hashes

Hashes for time_measure-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dc291b0f22f166bc375551d8306063cdb0d59bc2d90fa469210bc144e82847d1
MD5 5d87db3275ba052cf4be5dae0380f44d
BLAKE2b-256 75f67eb7c72e5223c321e1605450665801b9593e3fa1d7d1ea83d5766a17af70

See more details on using hashes here.

Supported by

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