การกำจัดลูป

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

Logan Thomas

Scientific Software Technical Trainer, Enthought

การวนลูปใน Python

  • รูปแบบการวนลูป:
    • ลูป for: วนซ้ำทีละรายการในลำดับ
    • ลูป while: วนซ้ำตราบที่เงื่อนไขเป็นจริง
    • ลูป "ซ้อนกัน": ใช้ลูปหนึ่งอยู่ภายในอีกลูป
    • สิ้นเปลืองทรัพยากร!
การเขียน Python Code ที่มีประสิทธิภาพ

ประโยชน์ของการกำจัดลูป

  • โค้ดสั้นลง
  • อ่านโค้ดได้ง่ายขึ้น
    • "แบบแบนดีกว่าแบบซ้อนกัน"
  • ประสิทธิภาพสูงขึ้น
การเขียน Python Code ที่มีประสิทธิภาพ

กำจัดลูปด้วยฟังก์ชันในตัว

# List of HP, Attack, Defense, Speed
poke_stats = [
    [90,  92, 75, 60],
    [25,  20, 15, 90],
    [65, 130, 60, 75],
    ...
]

alt="รายการโปเกมอน Abomasnow, Abra และ Absol พร้อมข้อมูล HP, Attack, Defense และ Speed ที่ถูกไฮไลต์"

การเขียน Python Code ที่มีประสิทธิภาพ
# List of HP, Attack, Defense, Speed
poke_stats = [
    [90,  92, 75, 60],
    [25,  20, 15, 90],
    [65, 130, 60, 75],
    ...
]

# For loop approach totals = [] for row in poke_stats: totals.append(sum(row))
# List comprehension totals_comp = [sum(row) for row in poke_stats]
# Built-in map() function totals_map = [*map(sum, poke_stats)]
การเขียน Python Code ที่มีประสิทธิภาพ
%%timeit
totals = []
for row in poke_stats:
    totals.append(sum(row))
140 µs ± 1.94 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit totals_comp = [sum(row) for row in poke_stats]
114 µs ± 3.55 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit totals_map = [*map(sum, poke_stats)]
95 µs ± 2.94 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
การเขียน Python Code ที่มีประสิทธิภาพ

กำจัดลูปด้วยโมดูลในตัว

poke_types = ['Bug', 'Fire', 'Ghost', 'Grass', 'Water']
# Nested for loop approach
combos = []
for x in poke_types:
    for y in poke_types:
        if x == y:
            continue
        if ((x,y) not in combos) & ((y,x) not in combos):
            combos.append((x,y))
# Built-in module approach
from itertools import combinations
combos2 = [*combinations(poke_types, 2)]
การเขียน Python Code ที่มีประสิทธิภาพ

กำจัดลูปด้วย NumPy

# Array of HP, Attack, Defense, Speed
import numpy as np

poke_stats = np.array([
    [90,  92, 75, 60],
    [25,  20, 15, 90],
    [65, 130, 60, 75],
    ...
])
การเขียน Python Code ที่มีประสิทธิภาพ

กำจัดลูปด้วย NumPy

avgs = []
for row in poke_stats:
    avg = np.mean(row)
    avgs.append(avg)

print(avgs)
[79.25, 37.5, 82.5, ...]
avgs_np = poke_stats.mean(axis=1)

print(avgs_np)
[ 79.25  37.5   82.5  ...]
การเขียน Python Code ที่มีประสิทธิภาพ

กำจัดลูปด้วย NumPy

%timeit avgs = poke_stats.mean(axis=1)
23.1 µs ± 235 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%%timeit
avgs = []
for row in poke_stats:
    avg = np.mean(row)
    avgs.append(avg)
5.54 ms ± 224 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
การเขียน Python Code ที่มีประสิทธิภาพ

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

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

Preparing Video For Download...