memory_profiler เป็นโค้ดสำหรับทำโปรไฟล์หน่วยความจำสำหรับโค้ดไพทอนโดยเฉพาะ
โดยรองรับทั้ง Python 2,3 รองรับ Windows , Linux และอื่น ๆ แถมเป็น BSD License (BSD)
สามารถติดตั้งได้โดยใช้คำสั่ง pip ดังนี้
pip install -U memory_profilerสำหรับ Windows ให้ทำการติดตั้งโดยใช้คำสั่ง
pip install -U https://github.com/pythonprofilers/memory_profiler/archive/master.zip
การใช้งาน
ให้ใส่ @profile ลงไปด้านบนฟังก์ชันที่ต้องการทำโปรไฟล์หน่วยความจำ ตัวอย่างเช่น@profile
def my_func():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
return a
if __name__ == '__main__':
my_func()
จากนั้น รันโดยใช้คำสั่ง
ผลลัพธ์python -m memory_profiler example.py
Line # Mem usage Increment Line Contents
==============================================
3 @profile
4 5.97 MB 0.00 MB def my_func():
5 13.61 MB 7.64 MB a = [1] * (10 ** 6)
6 166.20 MB 152.59 MB b = [2] * (2 * 10 ** 7)
7 13.61 MB -152.59 MB del b
8 13.61 MB 0.00 MB return a
อ่านเอกสารเพิ่มเติมได้ที่ https://pypi.org/project/memory_profiler/

0 ความคิดเห็น:
แสดงความคิดเห็น
แสดงความคิดเห็นได้ครับ :)