timers.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 
00003 import contextlib
00004 import os
00005 import sys
00006 import time
00007 
00008 
00009 class Timer(object):
00010     def __init__(self):
00011         self.start_time = time.time()
00012         self.start_clock = self._clock()
00013 
00014     def _clock(self):
00015         times = os.times()
00016         return times[0] + times[1]
00017 
00018     def __str__(self):
00019         return "[%.3fs CPU, %.3fs wall-clock]" % (
00020             self._clock() - self.start_clock,
00021             time.time() - self.start_time)
00022 
00023 
00024 @contextlib.contextmanager
00025 def timing(text, block=False):
00026     timer = Timer()
00027     if block:
00028         print "%s..." % text
00029     else:
00030         print "%s..." % text,
00031     sys.stdout.flush()
00032     yield
00033     if block:
00034         print "%s: %s" % (text, timer)
00035     else:
00036         print timer
00037     sys.stdout.flush()


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Mon Oct 6 2014 07:52:06