代码之家  ›  专栏  ›  技术社区  ›  Ram Rachum

有Python的高级分析模块吗?

  •  4
  • Ram Rachum  · 技术社区  · 14 年前

    我想分析我的Python代码。我很清楚 cProfile

    在飞行中 在运行程序时。

    heavy_func 在我的节目里。我想启动这个程序 重功能 重功能 在它运行的时候对自己进行分析。(如果您想知道如何在程序运行时操作东西:我可以从debug probe或集成到GUI应用程序中的shell进行操作。)

    2 回复  |  直到 14 年前
        1
  •  1
  •   Community CDub    7 年前

    this technique 应该帮助你找到“瓶颈”,这就是你想做的。 如果真正的问题在其他地方,它会告诉你他们在哪里。

    如果你想要一份冗长的原因清单, look here .

        2
  •  0
  •   Ram Rachum    14 年前

    我为此写了自己的模块。我叫它 cute_profile Here is the code . Here are the tests .

    Here is the blog post explaining how to use it.

    GarlicSim ,所以如果你想使用它,你可以 install garlicsim 做什么 from garlicsim.general_misc import cute_profile

    如果您想在python3代码中使用它,只需安装 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