change execution_time decorator

This commit is contained in:
Balshgit 2021-07-21 15:03:03 +03:00 committed by GitHub
parent a99094e6b1
commit e98ab90991
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,25 +13,30 @@ def func_exec_count(func):
return new_func return new_func
def execution_time(func): def execution_time(time_form='sec'):
multiply = {'sec': 1, 'min': 60, 'hour': 3600}
def wrapper(func):
@wraps(func) @wraps(func)
def exec_func(*args, **kwargs): def new_func(*args, **kwargs):
now = datetime.now() begin = datetime.now()
func_result = func(*args, **kwargs) result = func(*args, **kwargs)
end = datetime.now() end = datetime.now()
print('time to execute', (end - now).seconds) exec_time = (end - begin).seconds / multiply[time_form]
return func_result print(f'Duration, {time_form}: {exec_time}')
return exec_func return result
return new_func
return wrapper
@func_exec_count @func_exec_count
@execution_time @execution_time()
def summary(x: int, y: int) -> int: def summary(x: int, y: int) -> int:
z = x + y z = x + y
return z return z
@execution_time @execution_time(time_form='min')
def main(): def main():
result = 0 result = 0
for i in range(1, 11): for i in range(1, 11):