python で行単位でプロファイルを取得する方法のメモ。
行単位でプロファイルをとるには line_profiler を使います。
まず、line_profiler をインストールします。
次に、プロファイルをとりたいメソッドに @profile を付与します。
そして、プロファイルを取得するためにプログラムを実行します。
プロファイル結果が プログラム.py.lprof に出力され、以下で閲覧することができます。
各項目は以下の通り
Hits: 実行回数
Time: 合計の実行時間(単位はTimer unitの値)
Per Hit: 1回あたりの実行時間(単位はTimer unitの値)
% Time: 実行時間の割合
行単位でプロファイルをとるには line_profiler を使います。
まず、line_profiler をインストールします。
pip install line_profiler
次に、プロファイルをとりたいメソッドに @profile を付与します。
@profile def foo(x): ... def run(): ... if __name__ == '__main__': run()
そして、プロファイルを取得するためにプログラムを実行します。
kernprof -l {プログラム.py}
プロファイル結果が プログラム.py.lprof に出力され、以下で閲覧することができます。
python -m line_profiler {プログラム.py.lprof}
Timer unit: 1e-06 s Total time: 727.632 s File: foo.py Function: foo at line 399 Line # Hits Time Per Hit % Time Line Contents ============================================================== 399 @profile 401 def foo(): 402 403 7719789 176860582.0 22.9 24.3 score1 = calc_score1() 404 7719789 114940601.0 14.9 15.8 score2 = calc_score2() ...
各項目は以下の通り
Hits: 実行回数
Time: 合計の実行時間(単位はTimer unitの値)
Per Hit: 1回あたりの実行時間(単位はTimer unitの値)
% Time: 実行時間の割合