A lightweight Python decorator for measuring execution time and system resource usage.
Project description
time_it - Python Function Profiler
🚀 Overview
time_it is a lightweight Python decorator that measures execution time, memory usage, and CPU time of any function. It is designed to be efficient and flexible, allowing you to enable or disable profiling dynamically.
📌 Features
✅ Execution Time Measurement (High-precision timing with perf_counter)
✅ Memory Usage Tracking (Peak memory usage in MB)
✅ CPU Time Monitoring (User & System CPU time)
✅ Zero Overhead When Disabled (measure_time=False bypasses all profiling logic)
✅ Easy to Use as a Decorator (@time_it)
✅ Lightweight & No External Dependencies
📥 Installation
If using this as a standalone module, simply clone the repository:
git clone https://github.com/brianvess/time_it.git
cd time_it
Or install it as a package (when published to PyPI):
pip install time-it-profiler
📖 Usage
✅ Basic Example
from timeit_function import time_it
@time_it(measure_time=True) # Enable profiling
def sample_function():
total = sum(range(1, 1000000))
return total
sample_function()
Example Output:
Execution Time: 0.012345 seconds
Memory Usage: 1.23 MB
User CPU Time: 0.002345 seconds
System CPU Time: 0.000678 seconds
✅ Disable Profiling (Zero Overhead)
@time_it(measure_time=False) # No profiling, function runs normally
def quick_function():
return sum(range(1, 100))
quick_function()
🔍 How It Works
time_it Function
import time
import resource
from functools import wraps
def time_it(measure_time=True):
def decorator(func):
if not measure_time:
return func # Return the function unmodified if profiling is disabled
@wraps(func)
def wrapper(*args, **kwargs):
start_time = time.perf_counter()
start_resources = resource.getrusage(resource.RUSAGE_SELF)
result = func(*args, **kwargs)
end_time = time.perf_counter()
end_resources = resource.getrusage(resource.RUSAGE_SELF)
memory = (end_resources.ru_maxrss - start_resources.ru_maxrss) / 1024 # Convert KB to MB
user_time = end_resources.ru_utime - start_resources.ru_utime
system_time = end_resources.ru_stime - start_resources.ru_stime
print(
f"Execution Time: {end_time - start_time:.6f} seconds\\n"
f"Memory Usage: {memory:.2f} MB\\n"
f"User CPU Time: {user_time:.6f} seconds\\n"
f"System CPU Time: {system_time:.6f} seconds"
)
return result
return wrapper
return decorator
🛠️ Contributing
- 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! 🚀
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
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-0.0.1-py3-none-any.whl.
File metadata
- Download URL: time_it_profiler-0.0.1-py3-none-any.whl
- Upload date:
- Size: 3.5 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 |
60cc1bf73a46a14fa1843f344bb7ed9e0e17a7bf7e96aac46e6fa670e817bf93
|
|
| MD5 |
3d0a5d01801d0e1f0a67b195aa66e10e
|
|
| BLAKE2b-256 |
770bb7aa90bde253683f8a308025e8c8d9b97215dee8c1db0185e67debcd938e
|