4

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

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

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

是否已经编写了一个模块来做这样的事情?我可以自己写,但我只是想问一下,所以我不会重新发明轮子。

4

2 回答 2

1

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

如果您想要一份乏味的原因列表,请查看此处

于 2010-09-22T23:59:13.643 回答
0

我为此编写了自己的模块。我叫它cute_profile这是代码这是测试

这是解释如何使用它的博客文章。

它是GarlicSim的一部分,所以如果你想使用它,你可以安装garlicsim并执行from garlicsim.general_misc import cute_profile.

如果你想在 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 回答