Permalink
Browse files

add profile contextmanager

  • Loading branch information...
1 parent ae06aa0 commit 7972fe8826924ed67893aae1400326ca8799883f @cyberdelia cyberdelia committed Jun 10, 2012
Showing with 21 additions and 3 deletions.
  1. +8 −0 README.rst
  2. +13 −3 statprof.py
View
8 README.rst
@@ -34,6 +34,14 @@ It's easy to get started with ``statprof``: ::
statprof.stop()
statprof.display()
+Or with a contextmanager : ::
+
+ import statprof
+
+ with statprof.profile():
+ my_questionable_function()
+
+
For more comprehensive help, run ``pydoc statprof``.
View
16 statprof.py
@@ -100,15 +100,15 @@
significantly off if other threads' work patterns are not similar to the
main thread's work patterns.
"""
-
-
from __future__ import division
import os
import signal
+from contextlib import contextmanager
+
-__all__ = ['start', 'stop', 'reset', 'display']
+__all__ = ['start', 'stop', 'reset', 'display', 'profile']
###########################################################################
@@ -273,6 +273,16 @@ def reset(frequency=None):
state.reset(frequency)
+@contextmanager
+def profile():
+ start()
+ try:
+ yield
+ finally:
+ stop()
+ display()
+
+
###########################################################################
## Reporting API

0 comments on commit 7972fe8

Please sign in to comment.