0、
import time as t
class Mytimer:
def __init__(self):
self.unit = ['年','月','天','小时','分钟','秒']
self.borrow = [0,12,31,24,60,60]
self.prompt = '未开始计时'
self.lasted = []
self.begin = 0
self.end = 0
def __str__(self):
return self.prompt
__repr__ = __str__
def __add__(self,other):
prompt = '总共运行了'
result = []
for index in range(6):
result.append(self.lasted[index] + other.lasted[index])
if result[index]:
prompt += (str(result[index]) + self.unit[index])
return prompt
def start(self):
self.begin = t.localtime()
self.prompt = '提示:请先调用stop()进行计时'
print('计时开始')
def stop(self):
if not self.begin:
print('提示:请先调用start()进行计时')
else:
self.end = t.localtime()
self._calc()
print('计时结束')
def _calc(self):
self.lasted = []
self.prompt = '总共运行了'
for index in range(6):
temp = self.end[index] - self.begin[index]
if temp < 0:
i = 1
while self.lasted[index -i] < 1:#向前边的位数借
self.lasted[index - i] += self.borrow[index - i] - 1
self.lasted[index - i - 1] -= 1
i += 1#向更高位的借
self.lasted.append(self.borrow[index] + temp)
self.lasted[index-1] -= 1
else:
self.lasted.append(temp)
for index in range(6):
if self.lasted[index]:
self.prompt += str(self.lasted[index]) + self.unit[index]
self.begin = 0
self.end = 0
>>> t1 = Mytimer()
>>> t2 = Mytimer()
>>> t1.start()
计时开始
>>> t1.stop()
计时结束
>>> t1
总共运行了9秒
>>> t2.start()
计时开始
>>> t2.stop()
计时结束
>>> t2
总共运行了11秒
1、
import time as t
class Mytimer:
def __init__(self):
self.prompt = '未开始计时'
self.lasted = 0.0
self.begin = 0
self.end = 0
self.default_timer = t.perf_counter
def __str__(self):
return self.prompt
__repr__ = __str__
def __add__(self,other):
result = self.lasted +other.lasted
prompt = '总共运行了 %0.2f 秒' % result
return prompt
def start(self):
self.begin = self.default_timer()
self.prompt = '提示:请先调用stop()进行计时'
print('计时开始')
def stop(self):
if not self.begin:
print('提示:请先调用start()进行计时')
else:
self.end = self.default_timer()
self._calc()
print('计时结束')
def _calc(self):
self.lasted = self.end - self.begin
self.prompt = '总共运行了 %0.2f 秒' % self.lasted
self.begin = 0
self.end = 0
def set_timer(self,timer):
if timer == 'process_time':
self.default_timer = t.process_time
elif timer == 'perf_counter':
self.default_timer = t.perf_counter
else:
print('输入无效,请输入process_time或perf_counter')
>>> t1=Mytimer()
>>> t1.set_timer('perf_counter')
>>> t1.start()
计时开始
>>> t1.stop()
计时结束
>>> t1
总共运行了 16.37 秒
2、
import time as t
class Mytimer:
def __init__(self,func,number = 1000000):
self.prompt = '未开始计时'
self.lasted = 0.0
self.func = func
self.number = number
self.default_timer = t.perf_counter
def __str__(self):
return self.prompt
__repr__ = __str__
def __add__(self,other):
result = self.lasted +other.lasted
prompt = '总共运行了 %0.2f 秒' % result
return prompt
def timing(self):
self.begin = self.default_timer()
for i in range(self.number):
self.func()
self.end = self.default_timer()
self.lasted = self.end - self.begin
self.prompt = '总共运行了 %0.2f秒' % self.lasted
def set_timer(self,timer):
if timer == 'process_time':
self.default_timer = t.process_time
elif timer == 'perf_counter':
self.default_timer = t.perf_counter
else:
print('输入无效,请输入process_time或perf_counter')
>>> def test():
text = 'I love FishC.com!'
char = 'o'
if char in text:
pass
>>> t1=Mytimer(test)
>>> t1.timing()
>>> t1
总共运行了 0.16秒
>>> t2=Mytimer(test,100000000)
>>> t2.timing()
>>> t2
总共运行了 16.25秒
>>> t1+t2
'总共运行了 16.41 秒'
>>>