Escrevendo código Python eficiente
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%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)

Os dados deste exemplo são uma amostra aleatória de 35.000 heróis.
(não é o dataset original de 480 super-heróis)
%mprun -f convert_units convert_units(heroes, hts, wts)

Alocações pequenas podem resultar em 0.0 MiB na saída.
(usando o dataset original de 480 super-heróis)
%mprun -f convert_units convert_units(heroes, hts, wts)

Escrevendo código Python eficiente