Writing Efficient Python Code
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_profiler
pip install memory_profiler
memory_profiler
package%load_ext memory_profiler
%mprun -f convert_units convert_units(heroes, hts, wts)
memory_profiler
hero_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)
Data used in this example is a random sample of 35,000 heroes.
(not original 480 superheroes dataset)
%mprun -f convert_units convert_units(heroes, hts, wts)
Small memory allocations could result in 0.0 MiB
output.
(using original 480 superheroes dataset)
%mprun -f convert_units convert_units(heroes, hts, wts)
Writing Efficient Python Code