TimeGuardian
v0.0.7
TimeGuardian是一个Python软件包,旨在测量和记录函数的执行时间和内存使用情况。该软件包对于Python应用程序中的性能监视和优化特别有用。
直接从PIP安装TimeGuardian:
pip install timeguardian或者,从源代码安装:
cd path/to/TimeGuardian
pip install . 从timeguardian软件包中导入TimeGuardian ,并将其用作功能上的装饰器,以测量其执行时间和内存使用情况。
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 implementation通过有条件记录的监视性能:
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 implementation TimeGuardian软件包允许您为时间和内存测量设置全局单元。此功能使您可以自定义如何报告执行时间和内存使用量。
设置单位:使用TimeGuardian.set_time_unit(unit)和TimeGuardian.set_memory_unit(unit)设置全局单元以进行时间和内存测量。 unit参数应为一个字符串,指示所需的单元(例如,毫秒's's's's'bytes'for bytes'的字节,kb'kb'for kilobytes,'mb'mb'for megabytes)。
装饰功能:将@TimeGuardian.measure Decorator应用于您的功能。该装饰器将使用全球设置的单元来测量和记录执行时间和内存使用量。
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 ()在此示例中,以毫秒为单位测量和记录了sample_function ,以获取时间和兆字节以进行内存使用。
欢迎对TimeGuardian的贡献!
该项目是根据MIT许可证获得许可的 - 有关详细信息,请参见许可证文件。