Code profiling for runtime

Scrivere codice Python efficiente

Logan Thomas

Scientific Software Technical Trainer, Enthought

Code profiling

  • Detailed stats on frequency and duration of function calls
  • Line-by-line analyses
  • Package used: line_profiler
pip install line_profiler
Scrivere codice Python efficiente

Code profiling: runtime

heroes = ['Batman', 'Superman', 'Wonder Woman']

hts = np.array([188.0, 191.0, 183.0])

wts = np.array([ 95.0, 101.0,  74.0])
Scrivere codice Python efficiente
def convert_units(heroes, heights, weights):

    new_hts = [ht * 0.39370  for ht in heights]
    new_wts = [wt * 2.20462  for wt in weights]

    hero_data = {}

    for i,hero in enumerate(heroes):
        hero_data[hero] = (new_hts[i], new_wts[i])

    return hero_data
convert_units(heroes, hts, wts)
{'Batman': (74.0156, 209.4389),
 'Superman': (75.1967, 222.6666),
 'Wonder Woman': (72.0471, 163.1419)}
Scrivere codice Python efficiente

Code profiling: runtime

%timeit convert_units(heroes, hts, wts)
3 µs ± 32 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Scrivere codice Python efficiente
%timeit new_hts = [ht * 0.39370  for ht in hts]
1.09 µs ± 11 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit new_wts = [wt * 2.20462  for wt in wts]
1.08 µs ± 6.42 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%%timeit
hero_data = {}
for i,hero in enumerate(heroes):
    hero_data[hero] = (new_hts[i], new_wts[i])
634 ns ± 9.29 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Scrivere codice Python efficiente

Code profiling: line_profiler

Using line_profiler package

%load_ext line_profiler

Magic command for line-by-line times

%lprun -f
Scrivere codice Python efficiente

Code profiling: line_profiler

Using line_profiler package

%load_ext line_profiler

Magic command for line-by-line times

%lprun -f convert_units
Scrivere codice Python efficiente

Code profiling: line_profiler

Using line_profiler package

%load_ext line_profiler

Magic command for line-by-line times

%lprun -f convert_units convert_units(heroes, hts, wts)
Scrivere codice Python efficiente

%lprun output

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

alt=”Magic command lprun tabular output summarizing runtime profiling statistics”

Scrivere codice Python efficiente

%lprun output

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

alt=”Magic command lprun tabular output summarizing runtime profiling statistics with Line # and Hits columns highlighted”

Scrivere codice Python efficiente

%lprun output

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

alt=”Magic command lprun tabular output summarizing runtime profiling statistics with Time column highlighted”

Scrivere codice Python efficiente

%lprun output

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

alt=”Magic command lprun tabular output summarizing runtime profiling statistics with first line Time unit highlighted”

Scrivere codice Python efficiente

%lprun output

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

alt=”Magic command lprun tabular output summarizing runtime profiling statistics with line three highlighted”

Scrivere codice Python efficiente

%lprun output

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

alt=”Magic command lprun tabular output summarizing runtime profiling statistics with Per Hit column highlighted”

Scrivere codice Python efficiente

%lprun output

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

Magic command lprun tabular output summarizing runtime profiling statistics with line nine highlighted

Scrivere codice Python efficiente

%lprun output

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

alt=”Magic command lprun tabular output summarizing runtime profiling statistics with % Time column highlighted”

Scrivere codice Python efficiente

%lprun output

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

alt=”Magic command lprun tabular output summarizing runtime profiling statistics with Line Contents column highlighted”

Scrivere codice Python efficiente
%timeit convert_units convert_units(heroes, hts, wts)
3 µs ± 32 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%lprun -f convert_units convert_units(heroes, hts, wts)

alt="Magic command lprun tabular output summarizing runtime profiling statistics with second line Total time highlighted"

Scrivere codice Python efficiente

Let's practice your new profiling powers!

Scrivere codice Python efficiente

Preparing Video For Download...