(func=None, stream=sys.stderr, limit=100)
return do_profile() if func is None else do_profile()(func)
Profile a function or with block

    Good for local profiling where it is easy to inspect process output.

    :param func: Function to be decorated.
    :param stream: Output stream (default: `sys.stderr`).
    :param limit: `print_stats` restriction.

    Usage:

        # decorator with default parameters
        @profile
        def do_something(arg):
            ...

        # decorator with parameters
        @profile(stream=sys.stdout)
        def do_something(arg):
            ...

        # context manager with parameters
        with profile(stream=sys.stdout):
            ...
Profile a function or with block
Profile a function or with block
def profile(func=None, stream=sys.stderr, limit=100):
    """Profile a function or with block

    Good for local profiling where it is easy to inspect process output.

    :param func: Function to be decorated.
    :param stream: Output stream (default: `sys.stderr`).
    :param limit: `print_stats` restriction.

    Usage:

        # decorator with default parameters
        @profile
        def do_something(arg):
            ...

        # decorator with parameters
        @profile(stream=sys.stdout)
        def do_something(arg):
            ...

        # context manager with parameters
        with profile(stream=sys.stdout):
            ...
    """
    @contextmanager
    def do_profile():
        profile = cProfile.Profile()
        profile.enable()
        try:
            yield
        finally:
            profile.disable()
            stats = pstats.Stats(profile, stream=stream)
            stats.sort_stats('cumulative').print_stats(limit)

    return do_profile() if func is None else do_profile()(func)
def profile ( func = None , stream = sys . stderr , limit = 100 ) : @ contextmanager def do_profile ( ) : profile = cProfile . Profile ( ) profile . enable ( ) try : yield finally : profile . disable ( ) stats = pstats . Stats ( profile , stream = stream ) stats . sort_stats ( 'cumulative' ) . print_stats ( limit ) return do_profile ( ) if func is None else do_profile ( ) ( func )