การโปรไฟล์โค้ดสำหรับการใช้หน่วยความจำ

การเขียน Python Code ที่มีประสิทธิภาพ

Logan Thomas

Scientific Software Technical Trainer, Enthought

วิธีเร็วและตรงไปตรงมา

import sys
nums_list = [*range(1000)]
sys.getsizeof(nums_list)
9112
import numpy as np

nums_np = np.array(range(1000))
sys.getsizeof(nums_np)
8096
การเขียน Python Code ที่มีประสิทธิภาพ

การโปรไฟล์โค้ด: หน่วยความจำ

  • สถิติโดยละเอียดเกี่ยวกับการใช้หน่วยความจำ
  • วิเคราะห์ทีละบรรทัด
  • แพ็กเกจที่ใช้: memory_profiler
pip install memory_profiler
  • ใช้แพ็กเกจ memory_profiler
%load_ext memory_profiler

%mprun -f convert_units convert_units(heroes, hts, wts)
การเขียน Python Code ที่มีประสิทธิภาพ

การโปรไฟล์โค้ด: หน่วยความจำ

  • ต้องนำเข้าฟังก์ชันก่อนใช้ memory_profiler
    • hero_funcs.py

 

from hero_funcs import convert_units
%load_ext memory_profiler

%mprun -f convert_units convert_units(heroes, hts, wts)
การเขียน Python Code ที่มีประสิทธิภาพ

ผลลัพธ์ของ %mprun

%mprun -f convert_units convert_units(heroes, hts, wts)

alt="ผลลัพธ์แบบตารางของ magic command mprun สรุปสถิติการโปรไฟล์การใช้หน่วยความจำ"

การเขียน Python Code ที่มีประสิทธิภาพ

ผลลัพธ์ของ %mprun

%mprun -f convert_units convert_units(heroes, hts, wts)

alt="ผลลัพธ์แบบตารางของ magic command mprun สรุปสถิติการโปรไฟล์การใช้หน่วยความจำ โดยไฮไลต์คอลัมน์ Line #"

การเขียน Python Code ที่มีประสิทธิภาพ

ผลลัพธ์ของ %mprun

%mprun -f convert_units convert_units(heroes, hts, wts)

alt="ผลลัพธ์แบบตารางของ magic command mprun สรุปสถิติการโปรไฟล์การใช้หน่วยความจำ โดยไฮไลต์คอลัมน์ Mem usage"

การเขียน Python Code ที่มีประสิทธิภาพ

ผลลัพธ์ของ %mprun

%mprun -f convert_units convert_units(heroes, hts, wts)

alt="ผลลัพธ์แบบตารางของ magic command mprun สรุปสถิติการโปรไฟล์การใช้หน่วยความจำ โดยไฮไลต์คอลัมน์ Increment"

การเขียน Python Code ที่มีประสิทธิภาพ

ผลลัพธ์ของ %mprun

%mprun -f convert_units convert_units(heroes, hts, wts)

alt="ผลลัพธ์แบบตารางของ magic command mprun สรุปสถิติการโปรไฟล์การใช้หน่วยความจำ โดยไฮไลต์คอลัมน์ Line contents"

การเขียน Python Code ที่มีประสิทธิภาพ

ผลลัพธ์ของ %mprun

%mprun -f convert_units convert_units(heroes, hts, wts)

alt="ผลลัพธ์แบบตารางของ magic command mprun สรุปสถิติการโปรไฟล์การใช้หน่วยความจำ"

การเขียน Python Code ที่มีประสิทธิภาพ

ข้อควรระวังสำหรับผลลัพธ์ของ %mprun

%mprun -f convert_units convert_units(heroes, hts, wts)

alt="ผลลัพธ์แบบตารางของ magic command mprun สรุปสถิติการโปรไฟล์การใช้หน่วยความจำ โดยไฮไลต์หน่วย mebibytes (MiB) ในผลลัพธ์"

การเขียน Python Code ที่มีประสิทธิภาพ

ข้อควรระวังสำหรับผลลัพธ์ของ %mprun

ข้อมูลที่ใช้ในตัวอย่างนี้คือกลุ่มตัวอย่างสุ่มจากฮีโร่ 35,000 ตัว

(ไม่ใช่ชุดข้อมูลซูเปอร์ฮีโร่ 480 ตัวดั้งเดิม)

%mprun -f convert_units convert_units(heroes, hts, wts)

alt="ผลลัพธ์แบบตารางของ magic command mprun สรุปสถิติการโปรไฟล์การใช้หน่วยความจำ"

การเขียน Python Code ที่มีประสิทธิภาพ

ข้อควรระวังสำหรับผลลัพธ์ของ %mprun

การจัดสรรหน่วยความจำขนาดเล็กอาจให้ผลลัพธ์เป็น 0.0 MiB

(ใช้ชุดข้อมูลซูเปอร์ฮีโร่ 480 ตัวดั้งเดิม)

%mprun -f convert_units convert_units(heroes, hts, wts)

alt="ผลลัพธ์แบบตารางของ magic command mprun สรุปสถิติการโปรไฟล์การใช้หน่วยความจำ โดยทุกแถวในคอลัมน์ Increment แสดงค่า 0.0 MiB"

การเขียน Python Code ที่มีประสิทธิภาพ

ข้อควรระวังสำหรับผลลัพธ์ของ %mprun

  • ตรวจสอบหน่วยความจำโดยสอบถามจากระบบปฏิบัติการ
  • ผลลัพธ์อาจแตกต่างกันตามแพลตฟอร์มและการรันแต่ละครั้ง
    • ยังสามารถเปรียบเทียบการใช้หน่วยความจำของแต่ละบรรทัดได้
การเขียน Python Code ที่มีประสิทธิภาพ

มาฝึกกันเถอะ!

การเขียน Python Code ที่มีประสิทธิภาพ

Preparing Video For Download...