Guest User

Untitled

a guest
Feb 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. def benchmark(func):
  2. import time
  3. def wrapper(*args, **kwargs):
  4. t = time.clock()
  5. res = func(*args, **kwargs)
  6. print(func.__name__, time.clock() - t)
  7. return res
  8. return wrapper
  9.  
  10. def print_args(func):
  11. def wrapper(*args, **kwargs):
  12. res = func(*args, **kwargs)
  13. print(func.__name__, args, kwargs)
  14. return res
  15. return wrapper
  16.  
  17. def counter(func):
  18. """
  19. Count number of times a function is called.
  20. """
  21. def wrapper(*args, **kwargs):
  22. wrapper.count += 1 # In order to decorate the built-in functions.
  23. res = func(*args, **kwargs)
  24. print("function '{0}' was executed {1} times".format(func.__name__, wrapper.count))
  25. return res
  26. wrapper.count = 0
  27. return wrapper
  28.  
  29. @benchmark
  30. @print_args
  31. @counter
  32. def test1(a,b,c):
  33. print('test')
  34.  
  35. @benchmark
  36. @print_args
  37. @counter
  38. def test2(a,b,c):
  39. print(a,b,c)
  40.  
  41. for _ in range(10):
  42. test1(1,'2',c=(3,))
  43.  
  44. for _ in range(5):
  45. test2(1,'2',c=(3,))
  46.  
  47. for _ in range(10):
  48. test1(1,'2',c=(3,))
Add Comment
Please, Sign In to add comment