[英]Decorator's return is different from the actual return
為什么我得到不同的結果?
from time import time
def speed_test(func):
def wrapper(*args, **kwargs):
start = time()
func(*args, **kwargs)
end = time() - start
print(f"Execution time is: {end:.17f}s # Decorator")
return wrapper
@speed_test
def sorting(L):
L.sort() # For example
def sorting_1(L1):
L1.sort()
T = [10, 5, 3, 1, 0]
L = T.copy()
sorting(T)
a = time()
sorting_1(L)
b = time() - a
print(f"Execution time is: {b:.17f}s # Main")
Execution time is: 0.00000977516174316s # Decorator
Execution time is: 0.00000977516174316s # Main
Execution time is: 0.00000977516174316s # Decorator
Execution time is: 0.00000572204589844s # Main
為什么這兩個結果不同? 編輯:我的意思是這個..
在這里我想說兩件事:
time.perf_counter
對這樣的事情更有效率
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.