Created
September 13, 2017 15:49
-
-
Save edvardm/20d28c263662e385ccfa943c80c13445 to your computer and use it in GitHub Desktop.
Simple context manager for python cProfiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile, pstats, io | |
class MyProfiler: | |
def __init__(self): | |
self.pr = cProfile.Profile() | |
self.pr.enable() | |
def __enter__(self): | |
return self.pr | |
def __exit__(self, *args): | |
self.pr.disable() | |
s = io.StringIO() | |
ps = pstats.Stats(self.pr, stream=s).sort_stats('cumulative') | |
ps.print_stats() | |
print(s.getvalue()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment