TimeGuardian is a Python package designed for measuring and logging the execution time and memory usage of functions. This package is particularly useful for performance monitoring and optimization in Python applications.
Install TimeGuardian directly from pip:
pip install timeguardianOr, install from the source code:
cd path/to/TimeGuardian
pip install .Import TimeGuardian from the timeguardian package and use it as a decorator on your functions to measure their execution time and memory usage.
from timeguardian import TimeGuardian
# To measure execution time and/or memory usage
@TimeGuardian.measure
def my_function():
# function implementation
# Custom name logging
@TimeGuardian.measure(name="CustomName")
def another_function():
# function implementation
# Custom logging with name, time and memory
@TimeGuardian.measure(name="CustomName", elapsed=True, memory=True) #(elapsed in ms, memory in bytes)
def another_function():
# function implementationMonitor performance with conditional logging:
from timeguardian import TimeGuardian
# Monitor and log only if execution time or memory usage exceeds the specified limits (time in ms, memory in bytes)
@TimeGuardian.monitor(elapsed=200, memory=1024)
def monitored_function():
# function implementationThe TimeGuardian package allows you to set global units for time and memory measurements. This feature enables you to customize how the execution time and memory usage are reported.
Set the Units: Use TimeGuardian.set_time_unit(unit) and TimeGuardian.set_memory_unit(unit) to set the global units for time and memory measurement. The unit parameter should be a string indicating the desired unit (e.g., 'ms' for milliseconds, 's' for seconds, 'bytes' for bytes, 'KB' for kilobytes, 'MB' for megabytes).
Decorate Functions: Apply the @TimeGuardian.measure decorator to your functions. This decorator will use the globally set units to measure and log the execution time and memory usage.
from decorators import TimeGuardian
# Set the units for time and memory measurements
TimeGuardian.set_time_unit('ms') # Set time unit to milliseconds
TimeGuardian.set_memory_unit('MB') # Set memory unit to megabytes
@TimeGuardian.measure(elapsed=True, memory=True, name="Sample Function")
def sample_function():
# Function implementation
...
# Call the decorated function
sample_function()In this example, sample_function is measured and logged in milliseconds for time and megabytes for memory usage.
Contributions to TimeGuardian are welcome!
This project is licensed under the MIT License - see the LICENSE file for details.