KCacheGrind での cProfile 結果の使用
-
19-09-2019 - |
質問
cProfile を使用して Python プログラムのプロファイリングを行っています。に基づく この話 私は、KCacheGrind が cProfile からの出力を解析して表示できると信じていました。
ただし、ファイルをインポートしようとすると、KCacheGrind はステータス バーに「不明なファイル形式」エラーを表示するだけで、何も表示されずに放置されます。
プロファイリング統計が KCacheGrind と互換性を持つ前に、何か特別なことを行う必要がありますか?
...
if profile:
import cProfile
profileFileName = 'Profiles/pythonray_' + time.strftime('%Y%m%d_%H%M%S') + '.profile'
profile = cProfile.Profile()
profile.run('pilImage = camera.render(scene, samplePattern)')
profile.dump_stats(profileFileName)
profile.print_stats()
else:
pilImage = camera.render(scene, samplePattern)
...
パッケージのバージョン
- KCacheGrind 4.3.1
- Python 2.6.2
解決 3
これは lscallproftree の
と呼ばれる外部モジュールを使用して行うことができこの記事では、どのように説明します: CherryPyに - CacheGrindする
私の結果のコードはそうのように見えるしてます:
...
if profile:
import cProfile
import lsprofcalltree
profileFileName = 'Profiles/pythonray_' + time.strftime('%Y%m%d_%H%M%S') + '.profile'
profile = cProfile.Profile()
profile.run('pilImage = camera.render(scene, samplePattern)')
kProfile = lsprofcalltree.KCacheGrind(profile)
kFile = open (profileFileName, 'w+')
kProfile.output(kFile)
kFile.close()
profile.print_stats()
else:
pilImage = camera.render(scene, samplePattern)
...
誰もがこれを行う方法を知っている場合は、外部(すなわち。パイソンに同梱されていない)モジュールを必要としないことを、私はまだそれを聞いて非常に興味があると思います。
他のヒント
cProfileを使用すると、任意の別のプロファイリングスクリプトをすることなく、既存のプログラムをプロファイリングすることができます。ただ、プロファイラを使用してプログラムを実行します。
python -m cProfile -o profile_data.pyprof script_to_profile.py
-kスイッチpyprof2calltreeとkcachegrindにとオープンプロファイルデータを自動的kcachegrindでデータを開く
pyprof2calltree -i profile_data.pyprof -k
全体ペースターサーバーとWebアプリケーションのプロファイリング例えばこのように行われます。
python -m cProfile -o pyprof.out `which paster` serve development.ini
pyprof2calltreeはeasy_installを一緒にインストールすることができます。
profilestats.profile
のデコレータ($ pip install profilestats
)を使用することができます= "http://pypi.python.org/pypi/pyprof2calltree" REL = "noreferrer"> pyprof2calltree のモジュール(lsprofcalltree.py
のブランド変更)
from profilestats import profile
@profile
def func():
# do something here
スクリプトは通常どおりに実行することができます。 profilestats
は、二つのファイルを作成:。相応KCachegrind互換とcProfile形式でcachegrind.out.profilestats
とprofilestats.prof
を
あなたが実際にやろうとしていることのhref = "https://stackoverflow.com/questions/ <、あなたのコードの一部は、速度を最適化することができるもの見て、あなたがランダムにデバッガでそれを一時停止することができている場合375913 /何缶-I-使用・ツー・プロファイル-Cコードインのlinux / 378024#378024" >この方法はに動作します。それは意外かもしれないが、あなたは非常に多くのstackshotsは必要ありません。
3つの貴様のやり方ます:
I - CPROFILE
1 - (myfunc関数プロファイル)
ipythonからimport cProfile
filename = 'filename.prof'
cProfile.run('myfunc()', filename)
2 - あなたのシェルで使用可能なkcachegrindファイルにファイルを変換します。
sudo pip install pyprof2calltree
pyprof2calltree -i filename.prof -o callgrind.filename.prof
3 - kcachegrindで開くcallgrind.filename.prof
II - EMBEDDED CPROFILE
1 - 。あなたのコードに数行プロフィール
import cProfile
filename = 'filename.prof'
pr = cProfile.Profile()
pr.enable()
# ... lines to profile ...
pr.disable()
pr.dump_stats(filename)
2 - あなたのシェルで使用可能なkcachegrindファイルにファイルを変換します。
sudo pip install pyprof2calltree
pyprof2calltree -i filename.prof -o callgrind.filename.prof
3 - kcachegrindで開くcallgrind.filename.prof
III - YAPPI
1 - ipythonからか、あなたのコードからmyfunc関数を()のプロフィール
import yappi
filename = 'callgrind.filename.prof'
yappi.set_clock_type('cpu')
yappi.start(builtins=True)
myfunc()
stats = yappi.get_func_stats()
stats.save(filename, type='callgrind')
2 - kcachegrindで開くcallgrind.filename.prof