import cProfile
import io
import pstats


def profile(func):
    """ Profile the execution of a method """
    def wrapper(*args, **kwargs):
        prof = cProfile.Profile()
        prof.enable()
        retval = func(*args, **kwargs)
        prof.disable()
        stream = io.StringIO()
        stats = pstats.Stats(prof, stream=stream).sort_stats('cumtime')
        stats.print_stats()
        print(stream.getvalue())
        return retval
    return wrapper