55

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
4

5 に答える 5

95

cProfileを使用すると、個別のプロファイリングスクリプトを作成せずに、既存のプログラムをプロファイリングすることもできます。プロファイラーでプログラムを実行するだけ

python -m cProfile -o profile_data.pyprof script_to_profile.py

pyprof2calltreeを使用してkcachegrindのプロファイルデータを開きます。その-kスイッチはkcachegrindのデータを自動的に開きます

pyprof2calltree -i profile_data.pyprof -k

たとえば、pasterサーバーとwebapp全体のプロファイリングは次のように行われます。

python -m cProfile -o pyprof.out `which paster` serve development.ini

pyprof2calltreeはeasy_installでインストールできます。

于 2010-08-24T22:26:25.980 に答える
17

profilestats.profileデコレータ( )を使用できます-pyprof2calltreeモジュール$ pip install profilestatsの単純なラッパー(のブランド変更):lsprofcalltree.py

from profilestats import profile

@profile
def func():
    # do something here

スクリプトは通常どおり実行できます。profilestats2つのファイルを作成します:cachegrind.out.profilestatsそしてそれprofilestats.profに応じてKCachegrind互換およびcProfile形式で。

于 2010-03-28T22:01:10.103 に答える
7

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)
...

外部(つまり、Pythonに同梱されていない)モジュールを必要としないこれを行う方法を誰かが知っている場合でも、私はそれについて聞いて非常に興味があります。

于 2009-12-13T10:40:34.527 に答える
7

KCachegrind / Qcachegrindでコードをプロファイリングし、結果を視覚化する3つの異なる方法:

I-CPROFILE

1-ipythonからmyfunc()をプロファイルします

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-埋め込まれたプロファイル

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を開きます

于 2016-10-03T09:49:56.320 に答える
5

実際に実行しようとしているのが、コードのどの部分を速度に最適化できるかを確認し、デバッガーでランダムに一時停止できる場合、このメソッドは機能します。意外かもしれませんが、スタックショットはそれほど多くは必要ありません。

于 2009-12-14T22:59:28.223 に答える