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

def my_function():
    with 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

def my_function():
    time_measure_context = TimeMeasureContextManager()
    with time_measure_context():
        # Your code here
        sum(range(10**7))
    with time_measure_context():
        # More code
        sum(range(10**8))
    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.0.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.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: time-measure-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 9508168b8074d3b9f7f289af2582b47052a5918a1d93ec411644d3dcc98e3b9e
MD5 e4885cd8804775747e6da05cf2d62db7
BLAKE2b-256 0e10792cbb993a6eb3ede8a496d32cfc61f7719ff8ab070ecebae67b17668fdd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: time_measure-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 107a9c2df57be3639f22ff86b47e9836e2ed236dffd529358a130e932c73ae72
MD5 e7842e6d379412f1013adf0b8ff5e1f6
BLAKE2b-256 d8f2567ec966d37942983de5f713b03900484a2858e7f28bc181ac2b184e40ac

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