2
我試圖讓裝飾商可以在PythonPython:如何使用timeit?
def timer(func):
def smallfunctimer(*args,**kwargs):
res=func(*args,**kwargs)
tm=timeit.Timer(lambda:res)
print("Function time: ", tm)
return smallfunctimer
@timer
def deposit(self,amount):
self.balance+=amount
self.queue.append(BankTransaction(amount))
計數運行其他功能的時間的方法,但是當我把它稱爲
ba=BankAccount(1,100)
ba.deposit(10000000)
我得到這個:
Function time: <timeit.Timer object at 0x0281D370>
如何我可以在幾秒鐘內得到運行時間嗎?
非常感謝,它的工作原理! –