X-Git-Url: https://git.tld-linux.org/?p=TLD.git;a=blobdiff_plain;f=pld-builder.new%2FPLD_Builder%2Fstopwatch.py;fp=pld-builder.new%2FPLD_Builder%2Fstopwatch.py;h=151c164e4ff63bf8bc55b9950787b5b45cf40078;hp=0000000000000000000000000000000000000000;hb=90809c8fec988489786ce00247d9a4150070748b;hpb=ab3934fab858112cd552359b18cb980ea07c310b diff --git a/pld-builder.new/PLD_Builder/stopwatch.py b/pld-builder.new/PLD_Builder/stopwatch.py new file mode 100644 index 0000000..151c164 --- /dev/null +++ b/pld-builder.new/PLD_Builder/stopwatch.py @@ -0,0 +1,45 @@ +# vi: encoding=utf-8 ts=8 sts=4 sw=4 et + +import time +import resource + +class Time: + def __init__(self): + x = resource.getrusage(resource.RUSAGE_CHILDREN) + self.user_time = x[0] + self.sys_time = x[1] + self.non_io_faults = x[6] + self.io_faults = x[7] + self.time = time.time() + + def sub(self, x): + self.user_time -= x.user_time + self.sys_time -= x.sys_time + self.non_io_faults -= x.non_io_faults + self.io_faults -= x.io_faults + self.time -= x.time + + def format(self): + return "user:%.2fs sys:%.2fs real:%.2fs (faults io:%d non-io:%d)" % \ + (self.user_time, self.sys_time, self.time, self.io_faults, + self.non_io_faults) + +class Timer: + def __init__(self): + self.starts = [] + + def start(self): + self.starts.append(Time()) + + def stop(self): + tmp = Time() + tmp.sub(self.starts.pop()) + return tmp.format() + +t = Timer() + +def start(): + t.start() + +def stop(): + return t.stop()