diff --git a/decorators/decorator.py b/decorators/decorator.py index eab8160..c5dc44f 100644 --- a/decorators/decorator.py +++ b/decorators/decorator.py @@ -1,20 +1,17 @@ -#decorator -def my_decorator(func): +from time import time - def wrap_func(): - print('************') - func() - print('************') +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 - return wrap_func +@performance +def long_time(): + for i in range(1000000): + i*5 -@my_decorator -def hello(): - print('hellllooooooooooo') - -@my_decorator -def bye(): - print('see ya letter') - -hello() -bye() \ No newline at end of file +long_time() \ No newline at end of file diff --git a/decorators/wiki.txt b/decorators/wiki.txt index 2c06ca0..c9a5a08 100644 --- a/decorators/wiki.txt +++ b/decorators/wiki.txt @@ -108,4 +108,24 @@ 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() + + \ No newline at end of file