Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cProfile
- import pstats
- from StringIO import StringIO
- class profile(object):
- def __init__(self, sort_by='cumulative'):
- self.pr = cProfile.Profile()
- self.sort_by = sort_by
- def __enter__(self):
- self.pr.enable()
- self.s = StringIO()
- return self
- def __exit__(self, exc_type, exc_val, exc_tb):
- self.pr.disable()
- self.ps = pstats.Stats(self.pr, stream=self.s)\
- .sort_stats(self.sort_by)
- self.ps.print_stats()
- print(self.s.getvalue())
Add Comment
Please, Sign In to add comment