mirror of
https://github.com/Hizenberg469/Python-tutorial.git
synced 2026-04-19 15:12:22 +03:00
decorator finished
This commit is contained in:
@@ -1,20 +1,17 @@
|
|||||||
#decorator
|
from time import time
|
||||||
def my_decorator(func):
|
|
||||||
|
|
||||||
def wrap_func():
|
def performance(fn):
|
||||||
print('************')
|
def wrapper(*args, **kwargs):
|
||||||
func()
|
t1 = time()
|
||||||
print('************')
|
result = fn(*args, **kwargs)
|
||||||
|
t2 = time()
|
||||||
|
print(f'took {t2-t1} ms')
|
||||||
|
return result
|
||||||
|
return wrapper
|
||||||
|
|
||||||
return wrap_func
|
@performance
|
||||||
|
def long_time():
|
||||||
|
for i in range(1000000):
|
||||||
|
i*5
|
||||||
|
|
||||||
@my_decorator
|
long_time()
|
||||||
def hello():
|
|
||||||
print('hellllooooooooooo')
|
|
||||||
|
|
||||||
@my_decorator
|
|
||||||
def bye():
|
|
||||||
print('see ya letter')
|
|
||||||
|
|
||||||
hello()
|
|
||||||
bye()
|
|
||||||
@@ -108,4 +108,24 @@
|
|||||||
hello('hii')
|
hello('hii')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for ex:
|
||||||
|
from time import time
|
||||||
|
|
||||||
|
def performance(fn):
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
t1 = time()
|
||||||
|
result = fn(*args, **kwargs)
|
||||||
|
t2 = time()
|
||||||
|
print(f'took {t2-t1} ms')
|
||||||
|
return result
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
@performance
|
||||||
|
def long_time():
|
||||||
|
for i in range(1000000):
|
||||||
|
i*5
|
||||||
|
|
||||||
|
long_time()
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user