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許可證獲得許可的 - 有關詳細信息,請參見許可證文件。