Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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,))
Add Comment
Please, Sign In to add comment