Skip to content

Instantly share code, notes, and snippets.

@davesque
Created September 20, 2013 21:58
Show Gist options
  • Save davesque/6644474 to your computer and use it in GitHub Desktop.
Save davesque/6644474 to your computer and use it in GitHub Desktop.
Simple profiling context manager
import cProfile
import contextlib
import os
@contextlib.contextmanager
def profile(filename='~/python.profile', *args, **kwargs):
profile = cProfile.Profile(*args, **kwargs)
profile.enable()
yield
profile.disable()
if filename:
profile.dump_stats(os.path.expanduser(filename))
else:
profile.print_stats()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment