import time as t
class Myt():
def __init__(self):
self.begin = 0
self.end = 0
self.lasted = 0
self.prom = '请先开始计时'
def __str__(self):
return self.prom
def __add__(self, other):
result = self.lasted + other.lasted
prompt = '共运行了%0.2f秒' % result
return prompt
__repr__ = __str__
def start(self):
self.begin = t.perf_counter()
self.prom = '请关闭计时'
print('开始计时')
def stop(self):
if not self.begin:
self.prom = '请先开始计时'
else:
self.end = t.perf_counter()
self.__cla()
print('计时结束')
def __cla(self):
self.lasted = self.end - self.begin
self.prom = '总共运行了%0.2f秒' % self.lasted
print("end++", self.end, "\nbegin--", self.begin)
print("process_time=", t.process_time(), "perf_counter=", t.perf_counter())
self.begin = 0
self.end = 0
def set_time(self, timer):
if timer == 'process_time':
self.tt = t.process_time()
elif timer == 'per_counter':
self.tt = t.perf_counter()
else:
print("输入无效:请输入per_counter()或者process_time()")
t1 = Myt()
t1.start()