Efficiënte Python-code schrijven
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
memory_profilerpip install memory_profiler
memory_profiler gebruiken%load_ext memory_profiler
%mprun -f convert_units convert_units(heroes, hts, wts)
memory_profilerhero_funcs.py
from hero_funcs import convert_units
%load_ext memory_profiler
%mprun -f convert_units convert_units(heroes, hts, wts)
%mprun -f convert_units convert_units(heroes, hts, wts)

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

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

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

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

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

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

De data in dit voorbeeld is een willekeurige steekproef van 35.000 helden.
(niet de oorspronkelijke dataset met 480 superhelden)
%mprun -f convert_units convert_units(heroes, hts, wts)

Kleine geheugenallocaties kunnen resulteren in 0.0 MiB in de uitvoer.
(op de oorspronkelijke dataset met 480 superhelden)
%mprun -f convert_units convert_units(heroes, hts, wts)

Efficiënte Python-code schrijven