A lightweight Python decorator for measuring execution time and system resource usage.
Project description
🚀 time-it-profiler – Python Execution & Memory Profiler
time-it-profiler is a lightweight Python decorator and context manager for measuring execution time, memory usage, and CPU time of any function or code block. This updated version adds:
✔ Nanosecond Precision
✔ Function & Code Block Identification (Know exactly what was measured)
✔ Logging Support (log_file or logger)
✔ Context Manager Feature (with time_it():)
📌 Features
✅ High-Precision Execution Time (Uses time.perf_counter_ns())
✅ Memory Usage Tracking (Peak memory in MB)
✅ CPU Time Monitoring (User & System CPU time)
✅ Context Manager Support (with time_it(label="Block Name"):)
✅ Function Name in Reports (When used as a decorator)
✅ File & Logging Integration (@time_it(log_file="log.txt") or @time_it(logger=my_logger))
✅ Zero Overhead When Disabled (measure_time=False skips profiling)
✅ Lightweight & No External Dependencies
📥 Installation
Install directly from PyPI using:
pip install time-it-profiler
📂 GitHub Repository: https://github.com/brianvess/time_it.git
📖 Usage
✅ 1. As a Decorator (Tracks Function Execution)
from time_it import time_it
@time_it(measure_time=True)
def sample_function():
total = sum(range(1, 1000000))
return total
sample_function()
🔹 Example Output:
========================================
Function: sample_function
----------------------------------------
Execution Time: 0.012345 sec
Memory Usage: 1.23 MB
User CPU Time: 0.002345 sec
System CPU Time: 0.000678 sec
========================================
✅ 2. As a Context Manager (Tracks Code Blocks)
with time_it(label="Sum Calculation"):
result = sum(range(1, 1000000))
🔹 Example Output:
========================================
Code Block: Sum Calculation
----------------------------------------
Execution Time: 0.008765 sec
Memory Usage: 0.98 MB
User CPU Time: 0.001987 sec
System CPU Time: 0.000456 sec
========================================
✅ 3. With Logging to a File
@time_it(log_file="profile.log")
def test_logging():
time.sleep(1)
test_logging()
✅ 4. Using Python’s logging Module
import logging
logger = logging.getLogger("Profiler")
logging.basicConfig(level=logging.INFO)
@time_it(logger=logger)
def test_logger():
time.sleep(2)
test_logger()
🛠️ Contributing
Contributions are welcome! Fork the repository on GitHub and submit a pull request.
🔗 GitHub Repo: https://github.com/brianvess/time_it.git
-
Fork this repository.
-
Create a new branch:
git checkout -b feature-branch
-
Commit your changes:
git commit -m "Added new feature"
-
Push the changes:
git push origin feature-branch
-
Submit a pull request! 🎉
📜 License
This project is licensed under the MIT License. See LICENSE for details.
🌟 Credits
Developed by Brian Vess.
If you find this useful, please ⭐ star this repository and contribute! 🚀
What’s New in This Version?
✅ Function & Code Block Identification
✅ Better Output Formatting
✅ Same Lightweight Design – Now Even More Useful!
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file time_it_profiler-1.1.1.tar.gz.
File metadata
- Download URL: time_it_profiler-1.1.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43a91f46563fc1f94f2a8a1591dedf95c1636b2802ba6c0978617ddac912937e
|
|
| MD5 |
01f003eb73e6ab26e0b44f6fda2b19de
|
|
| BLAKE2b-256 |
e283dbbb910e9437bae0b5d0767c90e03e1ccd3c2c150f068d294fd085859a5c
|
File details
Details for the file time_it_profiler-1.1.1-py3-none-any.whl.
File metadata
- Download URL: time_it_profiler-1.1.1-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdd2b301786587c580e94f1bab9987198464c5736d9d70c16959ba8270aa2147
|
|
| MD5 |
afd7b45e5c111c7f754ee3a52f04a73b
|
|
| BLAKE2b-256 |
d1e958835930e4728430c357ea64a6aa40743f6f75de7cf95383d0ad7ecce03f
|