Skip to main content

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.

Metadata

Release files for time-measure 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for time-measure 0.1.1
File Size Uploaded Provenance
time-measure-0.1.1.tar.gz 4.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for time-measure 0.1.1
File Interpreter ABI Platform
time_measure-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.7 kB

Release files / time-measure-0.1.1.tar.gz

Download URL time-measure-0.1.1.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
86ff2a4a40585b69f26957c1e0c2d2aa501730e09757c38f8b623552c54798e9
BLAKE2b-256 checksum
How to use checksums
523fd1eca5293c8926ca890a778918e0942eaef1431bf5448c0eb20d2d62bcc3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.11

Release files / time_measure-0.1.1-py3-none-any.whl

Download URL time_measure-0.1.1-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dc291b0f22f166bc375551d8306063cdb0d59bc2d90fa469210bc144e82847d1
BLAKE2b-256 checksum
How to use checksums
75f67eb7c72e5223c321e1605450665801b9593e3fa1d7d1ea83d5766a17af70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.11

Security for time-measure 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page