def run_profile(func, sort_order="cumtime", count=1, strip_dir=True, name_filter=""):
"""sort_order : keywords 'ncalls', 'tottime', 'cumtime', 'filename' """
@wraps(func)
def wrapper(*args, **kwargs):
def cmd():
for i in range(count):
func(*args, **kwargs)
prof = cProfile.Profile()
_profile = prof.runctx("cmd()", globals(), locals())
stream = StringIO.StringIO()
stats = pstats.Stats(_profile, stream=stream)
if strip_dir:
stats.strip_dirs()
stats.sort_stats(sort_order)
stats.print_stats(name_filter)
return stream.getvalue()
return wrapper
评论列表
文章目录