[Assorted-commits] SF.net SVN: assorted: [229] python-commons/trunk/src/commons/misc.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-01-13 02:30:19
|
Revision: 229 http://assorted.svn.sourceforge.net/assorted/?rev=229&view=rev Author: yangzhang Date: 2008-01-12 18:30:22 -0800 (Sat, 12 Jan 2008) Log Message: ----------- added wall_clock Modified Paths: -------------- python-commons/trunk/src/commons/misc.py Modified: python-commons/trunk/src/commons/misc.py =================================================================== --- python-commons/trunk/src/commons/misc.py 2008-01-13 02:30:11 UTC (rev 228) +++ python-commons/trunk/src/commons/misc.py 2008-01-13 02:30:22 UTC (rev 229) @@ -1,6 +1,9 @@ # -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- # vim:ft=python:et:sw=4:ts=4 +from contextlib import * +from time import * + """ Miscellanea. """ @@ -21,3 +24,25 @@ for i in xrange( count ): yield j j <<= 1 + +@contextmanager +def wall_clock(output): + """ + A simple timer for code sections. + + @param output: The resulting time is put into index 0 of L{output}. + @type output: index-writeable + + Example: + + t = [0] + with wall_clock(t): + sleep(1) + print "the sleep operation took %d seconds" % t[0] + """ + start = time() + try: + yield + finally: + end = time() + output[0] = end - start This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |