実行時間のコードプロファイリング

効率的なPythonコードの書き方

Logan Thomas

Scientific Software Technical Trainer, Enthought

コードプロファイリング

  • 関数呼び出しの頻度と実行時間の詳細統計
  • 行単位の分析
  • 使用パッケージ:line_profiler
pip install line_profiler
効率的なPythonコードの書き方

コードプロファイリング:実行時間

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

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

wts = np.array([ 95.0, 101.0,  74.0])
効率的なPythonコードの書き方
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)}
効率的なPythonコードの書き方

コードプロファイリング:実行時間

%timeit convert_units(heroes, hts, wts)
3 µs ± 32 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
効率的なPythonコードの書き方
%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)
効率的なPythonコードの書き方

コードプロファイリング:line_profiler

line_profiler パッケージの使用

%load_ext line_profiler

行単位の時間を計測するマジックコマンド

%lprun -f
効率的なPythonコードの書き方

コードプロファイリング:line_profiler

line_profiler パッケージの使用

%load_ext line_profiler

行単位の時間を計測するマジックコマンド

%lprun -f convert_units
効率的なPythonコードの書き方

コードプロファイリング:line_profiler

line_profiler パッケージの使用

%load_ext line_profiler

行単位の時間を計測するマジックコマンド

%lprun -f convert_units convert_units(heroes, hts, wts)
効率的なPythonコードの書き方

%lprun の出力

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

alt="実行時間プロファイリング統計をまとめた lprun のタブラー出力"

効率的なPythonコードの書き方

%lprun の出力

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

alt="Line # と Hits 列が強調表示された lprun のタブラー出力"

効率的なPythonコードの書き方

%lprun の出力

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

alt="Time 列が強調表示された lprun のタブラー出力"

効率的なPythonコードの書き方

%lprun の出力

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

alt="1行目の Time unit が強調表示された lprun のタブラー出力"

効率的なPythonコードの書き方

%lprun の出力

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

alt="3行目が強調表示された lprun のタブラー出力"

効率的なPythonコードの書き方

%lprun の出力

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

alt="Per Hit 列が強調表示された lprun のタブラー出力"

効率的なPythonコードの書き方

%lprun の出力

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

9行目が強調表示された lprun のタブラー出力

効率的なPythonコードの書き方

%lprun の出力

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

alt="% Time 列が強調表示された lprun のタブラー出力"

効率的なPythonコードの書き方

%lprun の出力

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

alt="Line Contents 列が強調表示された lprun のタブラー出力"

効率的なPythonコードの書き方
%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="2行目の Total time が強調表示された lprun のタブラー出力"

効率的なPythonコードの書き方

新しいプロファイリングスキルを練習しましょう!

効率的なPythonコードの書き方

Preparing Video For Download...