]> TLD Linux GIT Repositories - TLD.git/blob - pld-builder.new/PLD_Builder/stopwatch.py
151c164e4ff63bf8bc55b9950787b5b45cf40078
[TLD.git] / pld-builder.new / PLD_Builder / stopwatch.py
1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
3 import time
4 import resource
5
6 class Time:
7     def __init__(self):
8         x = resource.getrusage(resource.RUSAGE_CHILDREN)
9         self.user_time = x[0]
10         self.sys_time = x[1]
11         self.non_io_faults = x[6]
12         self.io_faults = x[7]
13         self.time = time.time()
14
15     def sub(self, x):
16         self.user_time -= x.user_time
17         self.sys_time -= x.sys_time
18         self.non_io_faults -= x.non_io_faults
19         self.io_faults -= x.io_faults
20         self.time -= x.time
21
22     def format(self):
23         return "user:%.2fs sys:%.2fs real:%.2fs (faults io:%d non-io:%d)" % \
24                 (self.user_time, self.sys_time, self.time, self.io_faults,
25                  self.non_io_faults)
26
27 class Timer:
28     def __init__(self):
29         self.starts = []
30
31     def start(self):
32         self.starts.append(Time())
33
34     def stop(self):
35         tmp = Time()
36         tmp.sub(self.starts.pop())
37         return tmp.format()
38
39 t = Timer()
40
41 def start():
42     t.start()
43
44 def stop():
45     return t.stop()