Guest User

Untitled

a guest
Feb 21st, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. import cProfile
  2. import pstats
  3. from StringIO import StringIO
  4.  
  5. class profile(object):
  6. def __init__(self, sort_by='cumulative'):
  7. self.pr = cProfile.Profile()
  8. self.sort_by = sort_by
  9.  
  10. def __enter__(self):
  11. self.pr.enable()
  12. self.s = StringIO()
  13. return self
  14.  
  15. def __exit__(self, exc_type, exc_val, exc_tb):
  16. self.pr.disable()
  17. self.ps = pstats.Stats(self.pr, stream=self.s)\
  18. .sort_stats(self.sort_by)
  19. self.ps.print_stats()
  20. print(self.s.getvalue())
Add Comment
Please, Sign In to add comment