Add profile contextmanager #5

Merged
merged 1 commit into from Jun 11, 2012
View
@@ -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
@@ -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