我想介绍我的Python代码。我很了解 cProfile, ,我使用它,但是它太低了。 (例如,甚至没有一种直接的方法可以从要分析的函数中捕获返回值。)

我想做的一件事:我想在程序中扮演一个功能并将其设置为介绍 即时 运行程序时。

例如,假设我有一个功能 heavy_func 在我的程序中。我想启动该程序并拥有 heavy_func 功能本身不是概况。但是在我计划的运行时间里的某个时候,我想更改 heavy_func 在运行时概括自己。 (如果您想知道该程序运行时如何操纵内容:我可以从调试探测器或集成到GUI应用程序中的外壳中进行操作。)

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

有帮助吗?

解决方案 2

我为此写了自己的模块。我叫它 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

其他提示

这可能有点弯曲,但是 这项技术 应该帮助您找到“瓶颈”,这就是您想做的。您可以确定要关注的例程。如果这是您需要关注的例程,那将证明您是正确的。如果真正的问题在其他地方,它将向您展示它们在哪里。

如果您想要一个乏味的原因列表,为什么 看这里.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top