Guest User

Untitled

a guest
Jul 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import time
  5.  
  6. class timeit():
  7.  
  8. def __init__(self, label):
  9. import sys
  10. import time
  11. self.__time = time.time
  12. self.__label = label
  13.  
  14. def __call__(self, f, *args, **kwargs):
  15. self.__enter__()
  16. result = f(*args, **kwargs)
  17. self.__exit__()
  18. return result
  19.  
  20. def __enter__(self):
  21. self.ts = self.__time()
  22.  
  23. def __exit__(self, *args):
  24. te = self.__time()
  25. print('%s %2.2f ms' % (self.__label, (te - self.ts) * 1e3))
  26.  
  27. if __name__ == "__main__":
  28.  
  29. @timeit('function_label')
  30. def func():
  31. time.sleep(0.5)
  32.  
  33. for x in range(10):
  34. with timeit('anonymous_block_{0}'.format(x)):
  35. time.sleep(0.1)
  36.  
  37. with timeit('anonymous_block_noop'):
  38. pass
Add Comment
Please, Sign In to add comment