import time as t
class MyTimer:
def __init__(self, func, number=1000000):
self.prompt = "未开始计时!"
self.lasted = 0.0
self.default_timer = t.perf_counter
self.func = func
self.number = number
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
# 设置计时器(time.perf_counter() 或 time.process_time())
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("输入无效,请输入 perf_counter 或 process_time")
运行:>>> ================================ RESTART ================================
>>>
>>> def test():
text = "I love FishC.com!"
char = 'o'
if char in text:
pass
>>> t1 = MyTimer(test)
>>> t1.timing()
>>> t1
总共运行了 0.27 秒
>>> t2 = MyTimer(test, 100000000)
>>> t2.timing()
>>> t2
总共运行了 25.92 秒
>>> t1 + t2
'总共运行了 26.19 秒'
self.func() 在这有什么意义?
1、t1 = MyTimer(test) 的时候,传进了一个函数对象test
2、实例化类对象赋值给t1的时候,首先执行的是类初始化函数
class MyTimer:
def __init__(self, func, number=1000000):
self.prompt = "未开始计时!"
self.lasted = 0.0
self.default_timer = t.perf_counter
self.func = func # 这里把test这个函数对象赋值给了self.func
3、执行t1.timing()的时候:
def timing(self):
self.begin = self.default_timer()
for i in range(self.number):
self.func() # 这里执行的就是test了
self.end = self.default_timer()
self.lasted = self.end - self.begin
self.prompt = "总共运行了 %0.2f 秒" % self.lasted
|