Skip to content

Instantly share code, notes, and snippets.

@justinabrahms
Created July 22, 2010 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinabrahms/486336 to your computer and use it in GitHub Desktop.
Save justinabrahms/486336 to your computer and use it in GitHub Desktop.
# from __future__ import with_statement # py2.5
from contextlib import contextmanager
import logging
@contextmanager
def log_execution_time(log_msg):
"""
Context manager which logs the time spent doing a
particular activity.
Usage:
::
with log_execution_time("get response"):
self.login()
response = self.client.post(url, params)
self.assertRedirects(response, '/')
# Logs (to debug)
# get response: 0.10758805275
"""
start_time = time.time()
yield
logging.debug("%s: %s" % (log_msg, time.time() - start_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment