看代码理解python装饰器
< 返回列表时间: 2020-06-02来源:OSCHINA
以前使用的,查看函数运行时间的方法: #encoding:utf-8 import time def trainModel(): print("training...") time.sleep(2) def testModel(): print("testing...") time.sleep(1) if __name__ == "__main__": start = time.time() trainModel() #训练模型 end = time.time() print("cost time:",end-start) start = time.time() testModel() #测试模型 end = time.time() print("cost time:",end-start) """ training... cost time: 2.002230644226074 testing... cost time: 1.0011751651763916 """
或者是 #encoding:utf-8 import time def trainModel(): start = time.time() #写入函数内部 print("training...") end = time.time() print("cost time:",end-start) def testModel(): start = time.time() print("testing...") end = time.time() print("cost time:",end-start) if __name__ == "__main__": trainModel() #训练模型 testModel() #测试模型 """ training... cost time: 1.1682510375976562e-05 testing... cost time: 2.1457672119140625e-06 """
使用装饰器后发现真好用!!! 装饰器(装饰器为函数,被装饰的也是函数) #encoding:utf-8 import time def costTime(func): def wrapper(*args, **kwargs): start = time.time() f = func(*args, **kwargs) end = time.time() print("cost time:",end-start) return f return wrapper #相当于 costTime(trainModel()) #将trainModel拿到costTime中的wrapper中运行 #即可在trainModel()前后添加装饰代码 @costTime def trainModel(): print("training...") @costTime def testModel(): print("testing...") if __name__ == "__main__": trainModel() #训练模型 testModel() #测试模型 """ training... cost time: 1.1682510375976562e-05 testing... cost time: 2.1457672119140625e-06 """ #装饰器(装饰器为函数,被装饰的也是函数,传参数) #encoding:utf-8 import time def costTime(flag): def wrapper(func): def inner_wrapper(*args, **kwargs): start = time.time() f = func(*args, **kwargs) end = time.time() if flag == "True": print("cost time:",end-start) return f return inner_wrapper return wrapper @costTime(flag = "True") def trainModel(): print("training...") @costTime(flag = "False") def testModel(): print("testing...") if __name__ == "__main__": trainModel() #训练模型 testModel() #测试模型 """ training... cost time: 1.2159347534179688e-05 testing... """ #装饰器(装饰器为函数,被装饰的是类) #encoding:utf-8 import time def costTime(func): def wrapper(*args, **kwargs): start = time.time() f = func(*args, **kwargs) end = time.time() print("cost time:",end-start) return f return wrapper @costTime class trainModel: def __init__(self): print("training...") @costTime class testModel: def __init__(self): print("testing...") if __name__ == "__main__": trainModel() #训练模型 testModel() #测试模型 """ training... cost time: 1.2159347534179688e-05 testing... cost time: 2.86102294921875e-06 """ #装饰器(装饰器为函数,被装饰的是类,传参数) #encoding:utf-8 import time def costTime(flag): def wrapper(func): def inner_wrapper(*args, **kwargs): start = time.time() f = func(*args, **kwargs) end = time.time() if flag == "True": print("cost time:",end-start) return f return inner_wrapper return wrapper @costTime(flag = "True") class trainModel: def __init__(self): print("training...") @costTime(flag = "False") class testModel: def __init__(self): print("testing...") if __name__ == "__main__": trainModel() #训练模型 testModel() #测试模型 """ training... cost time: 1.1920928955078125e-05 testing... """ #装饰器(装饰器为类,被装饰的是函数) #encoding:utf-8 import time class costTime(): def __init__(self,func): self.fun = func #重载__call__方法是就需要接受一个函数并返回一个函数 def __call__(self,*args, **kwargs): start = time.time() f = self.fun(*args, **kwargs) end = time.time() print("cost time:",end-start) return f @costTime def trainModel(): print("training...") @costTime def testModel(): print("testing...") if __name__ == "__main__": trainModel() #训练模型 testModel() #测试模型 """ training... cost time: 1.1682510375976562e-05 testing... cost time: 2.1457672119140625e-06 """ #装饰器(装饰器为类,被装饰的也是函数,传参数) #encoding:utf-8 import time class costTime: def __init__(self,flag): self.flag = flag def __call__(self,func): def wrapper(*args, **kwargs): start = time.time() f = func(*args, **kwargs) end = time.time() if self.flag == "True": print("cost time:",end-start) return f return wrapper @costTime(flag = "True") def trainModel(): print("training...") @costTime(flag = "False") def testModel(): print("testing...") if __name__ == "__main__": trainModel() #训练模型 testModel() #测试模型 """ training... cost time: 1.2874603271484375e-05 testing... """ #装饰器(装饰器为类,被装饰的也是类) #encoding:utf-8 import time class costTime(): def __init__(self,func): self.fun = func #重载__call__方法是就需要接受一个函数并返回一个函数 def __call__(self,*args, **kwargs): start = time.time() f = self.fun(*args, **kwargs) end = time.time() print("cost time:",end-start) return f @costTime class trainModel: def __init__(self): print("training...") @costTime class testModel: def __init__(self): print("testing...") if __name__ == "__main__": trainModel() #训练模型 testModel() #测试模型 """ training... cost time: 1.2159347534179688e-05 testing... cost time: 2.86102294921875e-06 """ #装饰器(装饰器为函数,被装饰的是类,传参数) #encoding:utf-8 import time class costTime: def __init__(self,flag): self.flag = flag def __call__(self,func): def wrapper(*args, **kwargs): start = time.time() f = func(*args, **kwargs) end = time.time() if self.flag == "True": print("cost time:",end-start) return f return wrapper @costTime(flag = "True") class trainModel: def __init__(self): print("training...") @costTime(flag = "False") class testModel: def __init__(self): print("testing...") if __name__ == "__main__": trainModel() #训练模型 testModel() #测试模型 """ training... cost time: 1.1920928955078125e-05 testing... """
热门排行