使用CPROFILE介绍带有装饰器的代码的结果很奇怪
因此,我使用Cprofile和Snakeviz来详细说明我的代码。我的代码具有装饰器,这似乎使Cprofile结果不是很正确:
我的代码:
import time
import decorator
@decorator.decorator
def my_deco(f, *args, **kwargs):
ret = f(*args, **kwargs)
return ret
def a():
time.sleep(2)
b()
c()
@my_deco
def b():
time.sleep(3)
d()
@my_deco
def c():
time.sleep(1)
d()
@my_deco
def d():
time.sleep(3)
if __name__ == "__main__":
a()
没有装饰器的分析(预期行为): 在这种情况下,b,c处于同一水平,并且d bellow。
用装饰师进行分析(不想要行为): 在这种情况下,B,C和D处于同一级别。
So i'm using cProfile and snakeviz to profil my code. My code has decorator which seems to make the cProfile results not very correct :
my code :
import time
import decorator
@decorator.decorator
def my_deco(f, *args, **kwargs):
ret = f(*args, **kwargs)
return ret
def a():
time.sleep(2)
b()
c()
@my_deco
def b():
time.sleep(3)
d()
@my_deco
def c():
time.sleep(1)
d()
@my_deco
def d():
time.sleep(3)
if __name__ == "__main__":
a()
profiling without decorator (expected behavior) :
In this case, b, c are at the same level and d bellow.
profiling with decorator (not wanted behavior) :
In this case, b, c and d are at the same level.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论