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
@profileto any function to get detailed profiling output. - Line-by-Line Profiling:
Use
@profile.lineto obtain a detailed, line-by-line performance analysis (requiresline_profiler). - Context Manager:
Profile arbitrary code blocks with the provided
ProfileContext. - Command-Line Tool:
Run any Python script with
tprofilerto 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
timemodule to capture the execution time before and after function calls or code blocks. -
Memory Profiling:
Uses
psutilto measure the process's memory usage (RSS) before and after execution. -
Line Profiling:
Integrates with
line_profilerto 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.
Metadata
Release files for tprofiler 2.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tprofiler-2.0.0.tar.gz | 5.4 kB | Without provenance | Details for tprofiler-2.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tprofiler-2.0.0-py3-none-any.whl | Python 3 | none | any | Details for tprofiler-2.0.0-py3-none-any.whl |
Total release size: 11.4 kB
Release files / tprofiler-2.0.0.tar.gz
| Download URL | tprofiler-2.0.0.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
93031230468feb15685301fbfd4ebc5a0fd64da40d2ce48e8991b2958361ee19
|
|
BLAKE2b-256 checksum How to use checksums |
bb3bc535bd1397dc5bc56e0326a3e233c41b1d1b8d2f12500f8d2ceb76b87246
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.7
|
Release files / tprofiler-2.0.0-py3-none-any.whl
| Download URL | tprofiler-2.0.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
35c7ccc8c2929c77ec3359b89adc7e4251cebc9d424843a9e73b29acf6052a61
|
|
BLAKE2b-256 checksum How to use checksums |
f7327acf9f1117d1b6531285743b13276499226e100916d7cd795ae5bf40f49f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.7
|
Security for tprofiler 2.0.0
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.