Skip to main content

A combined time and memory profiler using psutil

Project description

tprofiler

tprofiler is a lightweight Python library for total profiling—combining time and memory profiling using psutil with optional line-by-line profiling using line_profiler. It provides a decorator for profiling individual functions, a context manager for profiling code blocks, and a command-line tool for profiling entire scripts.

Features

  • Combined Time and Memory Profiling: Track execution time and process memory (RSS) before and after function or code block execution.
  • Easy-to-Use Decorator: Simply add @profile to any function to get detailed profiling output.
  • Line-by-Line Profiling: Use @profile.line to obtain a detailed, line-by-line performance analysis (requires line_profiler).
  • Context Manager: Profile arbitrary code blocks with the provided ProfileContext.
  • Command-Line Tool: Run any Python script with tprofiler to obtain an overall profiling summary.

Installation

tprofiler is available on PyPI. Install it using pip:

pip install tprofiler

Usage

1. As a Decorator

Add profiling to any function by importing and applying the decorator:

from tprofiler.core import profile

@profile(enable_memory=True, enable_time=True, verbose=True)
def my_function(n):
    total = sum(range(n))
    return total

result = my_function(1000000)

When my_function is called, tprofiler prints the execution time and memory usage details, along with the function's return value if verbose is enabled.

2. Line-by-Line Profiling

For a detailed line-by-line analysis, use the line profiling decorator:

from tprofiler.core import profile

@profile.line
def compute_heavy(n):
    data = [i for i in range(n)]
    return sum(data)

compute_heavy(10_000_000)

This will output detailed line-by-line performance statistics for compute_heavy (ensure line_profiler is installed).

3. As a Command-Line Tool

You can profile an entire script by running:

tprofiler your_script.py [script arguments...]

For example, if you have a script named example.py, run:

tprofiler example.py --option value

This command executes the script and prints an overall profiling summary including total time elapsed and memory consumption.

4. Using the Context Manager

To profile a block of code without decorating a function, use the ProfileContext:

from tprofiler import ProfileContext

with ProfileContext(enable_memory=True, enable_time=True):
    # Place the code you want to profile here
    total = sum(range(1000000))
    print(total)

How It Works

  • Time Profiling:

    Uses Python's time module to capture the execution time before and after function calls or code blocks.

  • Memory Profiling:

    Uses psutil to measure the process's memory usage (RSS) before and after execution.

  • Line Profiling:

    Integrates with line_profiler to provide detailed per-line execution statistics.

Contributing

Contributions and improvements are welcome! Feel free to open issues or submit pull requests on GitHub.

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

tprofiler-1.0.3.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

tprofiler-1.0.3-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file tprofiler-1.0.3.tar.gz.

File metadata

  • Download URL: tprofiler-1.0.3.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for tprofiler-1.0.3.tar.gz
Algorithm Hash digest
SHA256 ae26cbef9bf2d22addc0b5435cacb1d7f958a5a5f3eba421da55607e1d6272e7
MD5 0353f0bcb951ef3f46cb231554f50686
BLAKE2b-256 47cc43a39973a8c4f831f8676506fabf4324c9bdfca2ce9a8c6d8c1436d045d4

See more details on using hashes here.

File details

Details for the file tprofiler-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: tprofiler-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for tprofiler-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ff34f8a97156da613d84d14571e45b0b57b70115825ee03549ebb0af0113f6be
MD5 6de78d511d80584064fc55d77154ef27
BLAKE2b-256 74d8d683fa69592bac9c80e64cca25fbc2223df33bfc4a45eb9378dae4caf279

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