Skip to content

Instantly share code, notes, and snippets.

@pylemon
Created May 21, 2014 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pylemon/8732f3a6817daff0af7c to your computer and use it in GitHub Desktop.
Save pylemon/8732f3a6817daff0af7c to your computer and use it in GitHub Desktop.
# coding: utf-8
"""
这是一个装饰器 可以检测函数调用中的各种性能参数
使用方法: @iprofile("cumulative", 20)
"""
import cProfile
import pstats
import StringIO
import functools
def iprofile(sortby='cumulative', topN=10):
def wrap(func):
@functools.wraps(func)
def wrapper(obj, *args, **kwargs):
pr = cProfile.Profile()
pr.enable()
result = func(obj, *args, **kwargs)
pr.disable()
s = StringIO.StringIO()
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats(topN)
print s.getvalue()
return result
return wrapper
return wrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment