Created
May 21, 2014 03:28
-
-
Save pylemon/8732f3a6817daff0af7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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