是否有Python的高级分析模块?

时间:2010-09-22 21:53:51

标签: python profiling introspection cprofile

我想描述我的Python代码。我很清楚cProfile,我使用它,但它太低了。 (例如,甚至没有一种简单的方法可以从您正在分析的函数中捕获返回值。)

我想做的事情之一:我想在我的程序中使用一个函数,并在运行程序时将其设置为动态

例如,假设我的程序中有一个函数heavy_func。我想启动该程序并让heavy_func函数不自行配置。但是在我的程序运行期间的某个时候,我希望在运行时将heavy_func更改为自我配置文件。 (如果你想知道如何在程序运行时操作东西:我可以从调试探针或集成到我的GUI应用程序的shell中完成它。)

是否已经编写了一个类似这样的模块?我可以自己写,但我之前想问,所以我不会重新发明轮子。

2 个答案:

答案 0 :(得分:1)

这可能有点令人费解,但this technique应该可以帮助您找到“瓶颈”,这就是您想要做的事情。 你非常确定你想要关注的例程。 如果那是你需要关注的例行程序,它将证明你是对的。 如果真正的问题在其他地方,它会告诉你它们在哪里。

如果您需要一系列繁琐的原因,look here

答案 1 :(得分:0)

我为它编写了自己的模块。我叫它cute_profileHere is the codeHere are the tests

Here is the blog post explaining how to use it.

这是GarlicSim的一部分,因此如果您想使用它,您可以install garlicsim并执行from garlicsim.general_misc import cute_profile

如果要在Python 3代码上使用它,只需安装Python 3 fork of 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