Skip to main content

计算函数运行时间

装饰器形式

# 创建一个装饰器函数
def func_timeit(count=1):
"""
计算一个函数式的执行时间

- param count=1 :{int} 重复多少次

@example
```python
@func_timeit(100)
def test(): pass
```
"""

def detector(func):
@wraps(func)
def run(*args, **key):
# 需要在func前运行的代码
start = time.time()

res = None
for i in range(count):
res = func(*args, **key)

end = time.time()
print(f"{func.__name__}[{count}] => {round(end-start, 3)}(ms)")

return res

return run

return detector