TimeGuardian เป็นแพ็คเกจ Python ที่ออกแบบมาสำหรับการวัดและบันทึกเวลาการดำเนินการและการใช้หน่วยความจำของฟังก์ชั่น แพ็คเกจนี้มีประโยชน์อย่างยิ่งสำหรับการตรวจสอบประสิทธิภาพและการเพิ่มประสิทธิภาพในแอปพลิเคชัน Python
ติดตั้ง TimeGuardian โดยตรงจาก PIP:
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 ควรเป็นสตริงที่ระบุหน่วยที่ต้องการ (เช่น 'MS' สำหรับมิลลิวินาที 'S' เป็นเวลาไม่กี่วินาที 'ไบต์' สำหรับไบต์ 'KB' สำหรับกิโลไบต์ 'MB' สำหรับ 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 ถูกวัดและเข้าสู่ระบบในมิลลิวินาทีสำหรับเวลาและ megabytes สำหรับการใช้หน่วยความจำ
ยินดีต้อนรับการมีส่วนร่วมใน TimeGuardian!
โครงการนี้ได้รับใบอนุญาตภายใต้ใบอนุญาต MIT - ดูไฟล์ใบอนุญาตสำหรับรายละเอียด