Eliminarea buclelor

Scriere eficientă a codului Python

Logan Thomas

Scientific Software Technical Trainer, Enthought

Buclele în Python

  • Tipare de buclare:
    • Bucla for: iterează secvențial
    • Bucla while: se repetă cât timp condiția este îndeplinită
    • Bucle „imbricate": o buclă în interiorul alteia
    • Costisitoare!
Scriere eficientă a codului Python

Avantajele eliminării buclelor

  • Mai puține linii de cod
  • Lizibilitate îmbunătățită
    • „Plat este mai bun decât imbricat"
  • Câștiguri de eficiență
Scriere eficientă a codului Python

Eliminarea buclelor cu funcții integrate

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

alt="Pokémon Abomasnow, Abra și Absol cu valorile lor de Sănătate, Atac, Apărare și Viteză evidențiate"

Scriere eficientă a codului Python
# 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)]
Scriere eficientă a codului Python
%%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)
Scriere eficientă a codului Python

Eliminarea buclelor cu module integrate

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)]
Scriere eficientă a codului Python

Eliminarea buclelor cu 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],
    ...
])
Scriere eficientă a codului Python

Eliminarea buclelor cu 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  ...]
Scriere eficientă a codului Python

Eliminarea buclelor cu 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)
Scriere eficientă a codului Python

Să exersăm!

Scriere eficientă a codului Python

Preparing Video For Download...