Schleifen vermeiden

Effizienten Python-Code schreiben

Logan Thomas

Scientific Software Technical Trainer, Enthought

Schleifen in Python

  • Schleifen-Muster:
    • for Schleife: Durchlaufe die Sequenz Element für Element.
    • while Schleife: Wiederhole die Schleife, solange die Bedingung erfüllt ist.
    • „Verschachtelte“ Schleifen: Verwendung einer Schleife innerhalb einer anderen Schleife.
    • Teuer!
Effizienten Python-Code schreiben

Vorteile der Vermeidung von Schleifen

  • Weniger Codezeilen
  • Bessere Lesbarkeit des Codes
    • „Flat is better than nested.“ (Flach ist besser als verschachtelt.)
  • Effizienzsteigerungen
Effizienten Python-Code schreiben

Schleifen mit integrierten Funktionen vermeiden

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

alt=”The Pokémon named Abomasnow, Abra, and Absol and their corresponding Health Points, Attack, Defense, and Speed metadata highlighted”

Effizienten Python-Code schreiben
# 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)]
Effizienten Python-Code schreiben
%%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)
Effizienten Python-Code schreiben

Schleifen mit eingebauten Modulen vermeiden

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)]
Effizienten Python-Code schreiben

Schleifen mit NumPy vermeiden

# 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],
    ...
])
Effizienten Python-Code schreiben

Schleifen mit NumPy vermeiden

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  ...]
Effizienten Python-Code schreiben

Schleifen mit NumPy vermeiden

%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)
Effizienten Python-Code schreiben

Lass uns üben!

Effizienten Python-Code schreiben

Preparing Video For Download...