147

私はコードのプロファイリングにcProfileを使用してきましたが、うまく機能しています。また、gprof2dot.pyを使用して結果を視覚化します(少し明確にします)。

ただし、cProfile(およびこれまでに見た他のほとんどのPythonプロファイラー)は、関数呼び出しレベルでのみプロファイリングするようです。これは、特定の関数が異なる場所から呼び出されるときに混乱を引き起こします-呼び出し#1または呼び出し#2が大部分の時間を占めているかどうかはわかりません。問題の関数が他の7つの場所から呼び出され、6レベルの深さである場合、これはさらに悪化します。

行ごとのプロファイリングを取得するにはどうすればよいですか?

これの代わりに:

function #12, total time: 2.0s

私はこのようなものを見たいです:

function #12 (called from somefile.py:102) 0.5s
function #12 (called from main.py:12) 1.5s

cProfileは、合計時間のどれだけが親に「転送」されるかを示しますが、多数のレイヤーと相互接続された呼び出しがある場合、この接続は失われます。

理想的には、データを解析し、各行に与えられた合計時間を含むソースファイルを表示するGUIが必要です。このようなもの:

main.py:

a = 1 # 0.0s
result = func(a) # 0.4s
c = 1000 # 0.0s
result = func(c) # 5.0s

次に、2番目の「func(c)」呼び出しをクリックして、「func(a)」呼び出しとは別に、その呼び出しで何が時間を取っているかを確認できます。それは理にかなっていますか?

4

5 に答える 5

134

それがRobertKernのline_profilerの目的だと思います。リンクから:

File: pystone.py
Function: Proc2 at line 149
Total time: 0.606656 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   149                                           @profile
   150                                           def Proc2(IntParIO):
   151     50000        82003      1.6     13.5      IntLoc = IntParIO + 10
   152     50000        63162      1.3     10.4      while 1:
   153     50000        69065      1.4     11.4          if Char1Glob == 'A':
   154     50000        66354      1.3     10.9              IntLoc = IntLoc - 1
   155     50000        67263      1.3     11.1              IntParIO = IntLoc - IntGlob
   156     50000        65494      1.3     10.8              EnumLoc = Ident1
   157     50000        68001      1.4     11.2          if EnumLoc == Ident1:
   158     50000        63739      1.3     10.5              break
   159     50000        61575      1.2     10.1      return IntParIO
于 2010-10-13T20:19:45.917 に答える
62

pprofilepypi )を使用することもできます。実行全体をプロファイリングする場合は、ソースコードを変更する必要はありません。2つの方法で、より大きなプログラムのサブセットをプロファイリングすることもできます。

  • 次のようなコード内の特定のポイントに到達したときにプロファイリングを切り替えます。

    import pprofile
    profiler = pprofile.Profile()
    with profiler:
        some_code
    # Process profile content: generate a cachegrind file and send it to user.
    
    # You can also write the result to the console:
    profiler.print_stats()
    
    # Or to a file:
    profiler.dump_stats("/tmp/profiler_stats.txt")
    
  • 統計的プロファイリングを使用して、コールスタックから非同期的にプロファイリングを切り替えます(たとえば、シグナルハンドラーや使用可能なワーカースレッドなど、考慮されるアプリケーションでこのコードをトリガーする方法が必要です)。

    import pprofile
    profiler = pprofile.StatisticalProfile()
    statistical_profiler_thread = pprofile.StatisticalThread(
        profiler=profiler,
    )
    with statistical_profiler_thread:
        sleep(n)
    # Likewise, process profile content
    

コード注釈の出力形式は、ラインプロファイラーによく似ています。

$ pprofile --threads 0 demo/threads.py
Command line: ['demo/threads.py']
Total duration: 1.00573s
File: demo/threads.py
File duration: 1.00168s (99.60%)
Line #|      Hits|         Time| Time per hit|      %|Source code
------+----------+-------------+-------------+-------+-----------
     1|         2|  3.21865e-05|  1.60933e-05|  0.00%|import threading
     2|         1|  5.96046e-06|  5.96046e-06|  0.00%|import time
     3|         0|            0|            0|  0.00%|
     4|         2|   1.5974e-05|  7.98702e-06|  0.00%|def func():
     5|         1|      1.00111|      1.00111| 99.54%|  time.sleep(1)
     6|         0|            0|            0|  0.00%|
     7|         2|  2.00272e-05|  1.00136e-05|  0.00%|def func2():
     8|         1|  1.69277e-05|  1.69277e-05|  0.00%|  pass
     9|         0|            0|            0|  0.00%|
    10|         1|  1.81198e-05|  1.81198e-05|  0.00%|t1 = threading.Thread(target=func)
(call)|         1|  0.000610828|  0.000610828|  0.06%|# /usr/lib/python2.7/threading.py:436 __init__
    11|         1|  1.52588e-05|  1.52588e-05|  0.00%|t2 = threading.Thread(target=func)
(call)|         1|  0.000438929|  0.000438929|  0.04%|# /usr/lib/python2.7/threading.py:436 __init__
    12|         1|  4.79221e-05|  4.79221e-05|  0.00%|t1.start()
(call)|         1|  0.000843048|  0.000843048|  0.08%|# /usr/lib/python2.7/threading.py:485 start
    13|         1|  6.48499e-05|  6.48499e-05|  0.01%|t2.start()
(call)|         1|   0.00115609|   0.00115609|  0.11%|# /usr/lib/python2.7/threading.py:485 start
    14|         1|  0.000205994|  0.000205994|  0.02%|(func(), func2())
(call)|         1|      1.00112|      1.00112| 99.54%|# demo/threads.py:4 func
(call)|         1|  3.09944e-05|  3.09944e-05|  0.00%|# demo/threads.py:7 func2
    15|         1|  7.62939e-05|  7.62939e-05|  0.01%|t1.join()
(call)|         1|  0.000423908|  0.000423908|  0.04%|# /usr/lib/python2.7/threading.py:653 join
    16|         1|  5.26905e-05|  5.26905e-05|  0.01%|t2.join()
(call)|         1|  0.000320196|  0.000320196|  0.03%|# /usr/lib/python2.7/threading.py:653 join

pprofileはコードの変更に依存しないため、トップレベルのモジュールステートメントをプロファイリングでき、プログラムの起動時間(モジュールのインポート、グローバルの初期化などにかかる時間)をプロファイリングできます。

cachegrind形式の出力を生成できるため、kcachegrindを使用して大きな結果を簡単に参照できます。

開示:私はpprofileの作者です。

于 2015-02-02T08:10:29.080 に答える
12

@JoeKingtonの上記の答えを改善するためだけに。

Python 3.xの場合、 line_profilerを使用します。


インストール:

pip install line_profiler

使用法:

プログラムmain.pyとその中に関数がfun_a()ありfun_b()、時間に関してプロファイリングしたいとします。@profile関数定義の直前にデコレータを使用する必要があります。たとえば、

@profile
def fun_a():
    #do something

@profile
def fun_b():
    #do something more

if __name__ == '__main__':
    fun_a()
    fun_b()

プログラムは、シェルコマンドを実行することでプロファイリングできます。

$ kernprof -l -v main.py

引数は、を使用してフェッチできます$ kernprof -h

Usage: kernprof [-s setupfile] [-o output_file_path] scriptfile [arg] ...

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -l, --line-by-line    Use the line-by-line profiler from the line_profiler
                        module instead of Profile. Implies --builtin.
  -b, --builtin         Put 'profile' in the builtins. Use 'profile.enable()'
                        and 'profile.disable()' in your code to turn it on and
                        off, or '@profile' to decorate a single function, or
                        'with profile:' to profile a single section of code.
  -o OUTFILE, --outfile=OUTFILE
                        Save stats to <outfile>
  -s SETUP, --setup=SETUP
                        Code to execute before the code to profile
  -v, --view            View the results of the profile in addition to saving
                        it.

結果はコンソールに次のように出力されます。

Total time: 17.6699 s
File: main.py
Function: fun_a at line 5

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    5                                           @profile
    6                                           def fun_a():
...


編集:プロファイラーからの結果は、TAMPPAパッケージを使用して解析できます。それを使用して、次のように行ごとの目的のプロットを取得できます プロット

于 2020-06-15T07:02:09.630 に答える
8

あなたはこれのためにline_profilerパッケージの助けを借りることができます

1.最初にパッケージをインストールします。

    pip install line_profiler

2. magicコマンドを使用して、パッケージをpython/notebook環境にロードします

    %load_ext line_profiler

3.関数のコードをプロファイリングする場合
は、次のようにします。

    %lprun -f demo_func demo_func(arg1, arg2)

これらの手順に従うと、すべての詳細が記載された適切な形式の出力が得られます:)

Line #      Hits      Time    Per Hit   % Time  Line Contents
 1                                           def demo_func(a,b):
 2         1        248.0    248.0     64.8      print(a+b)
 3         1         40.0     40.0     10.4      print(a)
 4         1         94.0     94.0     24.5      print(a*b)
 5         1          1.0      1.0      0.3      return a/b
于 2019-06-28T06:03:40.333 に答える
1

PyVmMonitorには、そこで役立つライブビューがあります(実行中のプログラムに接続して、そこから統計を取得できます)。

参照:http ://www.pyvmmonitor.com/

于 2015-04-28T23:12:07.343 に答える