4

Pythonコードのプロファイルを作成したいと思います。私はよく知ってcProfileいて、それを使用していますが、レベルが低すぎます。(たとえば、プロファイリングしている関数からの戻り値を取得する簡単な方法すらありません。)

私がやりたいことの1つは、プログラムの関数を取得し、プログラムの実行中にその場でプロファイルされるように設定したいことです。

たとえばheavy_func、プログラムに関数があるとします。heavy_funcプログラムを起動して、関数自体をプロファイルしないようにしたい。heavy_funcしかし、プログラムの実行中に、実行中にプロファイル自体に変更したい場合があります。(プログラムの実行中にどのように操作できるか疑問に思っている場合は、デバッグプローブから、またはGUIアプリに統合されているシェルから操作できます。)

このようなことを行うモジュールはすでに書かれていますか?自分で書くことはできますが、前に聞きたかったので、車輪の再発明はしません。

4

2 に答える 2

1

少し気が遠くなるかもしれませんが、このテクニックは「ボトルネック」を見つけるのに役立つはずです。それがあなたがやりたいことです。どのルーチンに焦点を当てたいかはかなり明確です。それがあなたが集中する必要があるルーチンであるなら、それはあなたが正しいことを証明するでしょう. 実際の問題が別の場所にある場合は、その場所が表示されます。

面倒な理由のリストが必要な場合は、こちらをご覧ください

于 2010-09-22T23:59:13.643 に答える
0

そのための独自のモジュールを作成しました。私はそれを呼んだcute_profileここにコードがありますここにテストがあります

使い方を解説したブログ記事はこちら。

これはGarlicSimの一部なので、使いたい場合はインストールgarlicsimして実行できますfrom garlicsim.general_misc import cute_profile

Python 3 コードで使用する場合は、 のPython 3 フォークをインストールするだけですgarlicsim

コードからの古い抜粋を次に示します。

import functools

from garlicsim.general_misc import decorator_tools

from . import base_profile


def profile_ready(condition=None, off_after=True, sort=2):
    '''
    Decorator for setting a function to be ready for profiling.

    For example:

        @profile_ready()
        def f(x, y):
            do_something_long_and_complicated()

    The advantages of this over regular `cProfile` are:

     1. It doesn't interfere with the function's return value.

     2. You can set the function to be profiled *when* you want, on the fly.

    How can you set the function to be profiled? There are a few ways:

    You can set `f.profiling_on=True` for the function to be profiled on the
    next call. It will only be profiled once, unless you set
    `f.off_after=False`, and then it will be profiled every time until you set
    `f.profiling_on=False`.

    You can also set `f.condition`. You set it to a condition function taking
    as arguments the decorated function and any arguments (positional and
    keyword) that were given to the decorated function. If the condition
    function returns `True`, profiling will be on for this function call,
    `f.condition` will be reset to `None` afterwards, and profiling will be
    turned off afterwards as well. (Unless, again, `f.off_after` is set to
    `False`.)

    `sort` is an `int` specifying which column the results will be sorted by.
    '''


    def decorator(function):

        def inner(function_, *args, **kwargs):

            if decorated_function.condition is not None:

                if decorated_function.condition is True or \
                   decorated_function.condition(
                       decorated_function.original_function,
                       *args,
                       **kwargs
                       ):

                    decorated_function.profiling_on = True

            if decorated_function.profiling_on:

                if decorated_function.off_after:
                    decorated_function.profiling_on = False
                    decorated_function.condition = None

                # This line puts it in locals, weird:
                decorated_function.original_function

                base_profile.runctx(
                    'result = '
                    'decorated_function.original_function(*args, **kwargs)',
                    globals(), locals(), sort=decorated_function.sort
                )                
                return locals()['result']

            else: # decorated_function.profiling_on is False

                return decorated_function.original_function(*args, **kwargs)

        decorated_function = decorator_tools.decorator(inner, function)

        decorated_function.original_function = function
        decorated_function.profiling_on = None
        decorated_function.condition = condition
        decorated_function.off_after = off_after
        decorated_function.sort = sort

        return decorated_function

    return decorator
于 2010-12-23T22:47:09.083 に答える