1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
""" Module for keeping detailed information about
times of certain driver parts
"""
import time
import py
from rpython.tool.ansi_print import AnsiLogger
log = AnsiLogger("Timer")
class Timer(object):
def __init__(self, timer=time.time):
self.events = []
self.next_even = None
self.timer = timer
self.t0 = None
def start_event(self, event):
now = self.timer()
if self.t0 is None:
self.t0 = now
self.next_event = event
self.start_time = now
def end_event(self, event):
assert self.next_event == event
now = self.timer()
self.events.append((event, now - self.start_time))
self.next_event = None
self.tk = now
def ttime(self):
try:
return self.tk - self.t0
except AttributeError:
return 0.0
def pprint(self):
""" Pretty print
"""
spacing = " "*(30 - len("Total:"))
total = "Total:%s --- %.1f s" % (spacing, self.ttime())
log.bold("Timings:")
for event, time in self.events:
spacing = " "*(30 - len(event))
first = "%s%s --- " % (event, spacing)
second = "%.1f s" % (time,)
additional_spaces = " " * (len(total) - len(first) - len(second))
log.bold("%s%s%s" % (first, additional_spaces, second))
log.bold("=" * len(total))
log.bold(total)
|