Codeprofiling voor runtimes

Efficiënte Python-code schrijven

Logan Thomas

Scientific Software Technical Trainer, Enthought

Codeprofiling

  • Gedetailleerde stats over frequentie en duur van functieruns
  • Analyse per regel
  • Gebruikt pakket: line_profiler
pip install line_profiler
Efficiënte Python-code schrijven

Codeprofiling: runtime

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

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

wts = np.array([ 95.0, 101.0,  74.0])
Efficiënte Python-code schrijven
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)}
Efficiënte Python-code schrijven

Codeprofiling: runtime

%timeit convert_units(heroes, hts, wts)
3 µs ± 32 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Efficiënte Python-code schrijven
%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)
Efficiënte Python-code schrijven

Codeprofiling: line_profiler

Met het pakket line_profiler

%load_ext line_profiler

Magic-opdracht voor tijden per regel

%lprun -f
Efficiënte Python-code schrijven

Codeprofiling: line_profiler

Met het pakket line_profiler

%load_ext line_profiler

Magic-opdracht voor tijden per regel

%lprun -f convert_units
Efficiënte Python-code schrijven

Codeprofiling: line_profiler

Met het pakket line_profiler

%load_ext line_profiler

Magic-opdracht voor tijden per regel

%lprun -f convert_units convert_units(heroes, hts, wts)
Efficiënte Python-code schrijven

%lprun-output

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

alt="Magic-opdracht lprun: tabeloutput met runtime-profileringsstatistieken"

Efficiënte Python-code schrijven

%lprun-output

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

alt="Magic-opdracht lprun: tabeloutput met runtime-profileringsstatistieken met kolommen Line # en Hits gemarkeerd"

Efficiënte Python-code schrijven

%lprun-output

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

alt="Magic-opdracht lprun: tabeloutput met runtime-profileringsstatistieken met kolom Time gemarkeerd"

Efficiënte Python-code schrijven

%lprun-output

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

alt="Magic-opdracht lprun: tabeloutput met runtime-profileringsstatistieken met eerste regel Time-eenheid gemarkeerd"

Efficiënte Python-code schrijven

%lprun-output

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

alt="Magic-opdracht lprun: tabeloutput met runtime-profileringsstatistieken met regel drie gemarkeerd"

Efficiënte Python-code schrijven

%lprun-output

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

alt="Magic-opdracht lprun: tabeloutput met runtime-profileringsstatistieken met kolom Per Hit gemarkeerd"

Efficiënte Python-code schrijven

%lprun-output

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

Magic-opdracht lprun: tabeloutput met runtime-profileringsstatistieken met regel negen gemarkeerd

Efficiënte Python-code schrijven

%lprun-output

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

alt="Magic-opdracht lprun: tabeloutput met runtime-profileringsstatistieken met kolom % Time gemarkeerd"

Efficiënte Python-code schrijven

%lprun-output

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

alt="Magic-opdracht lprun: tabeloutput met runtime-profileringsstatistieken met kolom Line Contents gemarkeerd"

Efficiënte Python-code schrijven
%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-opdracht lprun: tabeloutput met runtime-profileringsstatistieken met tweede regel Total time gemarkeerd"

Efficiënte Python-code schrijven

Laten we oefenen!

Efficiënte Python-code schrijven

Preparing Video For Download...