Pythonメモ : pyheatで時間のかかる処理をヒートマップで表示
pyheat
pyheatはpprofile + matplotlibのようなモジュールで、pprofile(line_profilerのようにコード単位でプロファイリング)のプロファイリング結果をヒートマップで表示することができる。
インストール
pipでインストールできるので下記コマンドを実行(インストールはpyheat
ではなくpy-heat
なので注意)。
$ pip install py-heat
ヘルプメッセージ
$ pyheat --help usage: pyheat [-h] [-o OUT] pyfile positional arguments: pyfile Python file to be profiled optional arguments: -h, --help show this help message and exit -o OUT, --out OUT Output file
使い方
対象ファイルを指定して実行すると画像ビューアで結果が表示される。
$ pyheat test_program.py
ファイル出力したい場合は--out
オプションで指定する(使用したテストコードはここ)。
$ pyheat test_program.py --out image_file.png
モジュールとして使う場合は以下のようにする。
from pyheat import PyHeat ph = PyHeat(<file_path>) ph.create_heatmap() # To view the heatmap. ph.show_heatmap() # To output the heatmap as a file. ph.show_heatmap('image_file.png')
pyheatmagic(IPythonで使う場合)
pyheatmagic
はpyheat
をIPythonで使用するモジュール。
インストール
pipでインストールできるので下記コマンドを実行。
$ pip install py-heat-magic
使い方
%load_ext heat
でロードして、%%heat
の後に対象コードを記述して実行すればよい。ファイル出力する場合は%%heat -o file.png
のようにする。