Created
February 12, 2018 20:37
-
-
Save pavelpy/f734c0debd4fbe494713d505340b00ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def benchmark(func): | |
import time | |
def wrapper(*args, **kwargs): | |
t = time.clock() | |
res = func(*args, **kwargs) | |
print(func.__name__, time.clock() - t) | |
return res | |
return wrapper | |
def print_args(func): | |
def wrapper(*args, **kwargs): | |
res = func(*args, **kwargs) | |
print(func.__name__, args, kwargs) | |
return res | |
return wrapper | |
def counter(func): | |
""" | |
Count number of times a function is called. | |
""" | |
def wrapper(*args, **kwargs): | |
wrapper.count += 1 # In order to decorate the built-in functions. | |
res = func(*args, **kwargs) | |
print("function '{0}' was executed {1} times".format(func.__name__, wrapper.count)) | |
return res | |
wrapper.count = 0 | |
return wrapper | |
@benchmark | |
@print_args | |
@counter | |
def test1(a,b,c): | |
print('test') | |
@benchmark | |
@print_args | |
@counter | |
def test2(a,b,c): | |
print(a,b,c) | |
for _ in range(10): | |
test1(1,'2',c=(3,)) | |
for _ in range(5): | |
test2(1,'2',c=(3,)) | |
for _ in range(10): | |
test1(1,'2',c=(3,)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment