from django.test import TestCase# Create your tests here.import time复制代码
# def deco(func):# def wrapper():# startTime = time.time()# func()# endTime = time.time()# msecs = (endTime - startTime)*1000# print('time is %d ms' %msecs)# return wrapper# @deco# def func():# print('hello')# time.sleep(2)# print('world')复制代码
# def deco(func):# def wapper(a,b):# startTime = time.time()# func(a,b)# endTime = time.time()# msecs = (endTime-startTime)*1000# print('用时----- %d ms' %msecs)# return wapper## @deco# def func(a,b):# print('需要执行的函数 :')# time.sleep(1)# print('执行结果 %d' %(a*b))复制代码
def deco1(func): def wapper(*args,**kwargs): print('第一个装饰器') startTime = time.time() func(*args,**kwargs) endTime = time.time() allTime = (endTime-startTime)*1000 print('总共用时',allTime) return wapperdef deco2(func): def wapper(*args, **kwargs): print('第二个装饰器') startTime = time.time() func(*args, **kwargs) endTime = time.time() allTime = (endTime - startTime) * 1000 print('总共用时', allTime) return wapper@deco1@deco2def func(a,b): print('函数执行') time.sleep(3) print('结果是 %d'%(a*b)) 复制代码
def deco1(func): def wapper(*args,**kwargs): print('装饰器') start_Time = time.time() str1 = func(*args,**kwargs) end_Time = time.time() all_time = (end_Time-start_Time)*1000 print('总共用时',all_time) return str1 return wapper@deco1def func(a,b): print('aaaaaaaaaaaaa') time.sleep(2) print('bbbbbbbbbbb') return a+b 复制代码
if __name__ == '__main__': func(1,10)复制代码