|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
问题1:没看懂小甲鱼给出的答案的作用
问题2:process_time 跟 perf_counter到底怎么用
- 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 __repr__(self):
- return self.prompt
- 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")
复制代码
2个都是连续运行差值给出时间,单位是秒
perf_counter是程序运行时间
process_time是CPU有效运行时间(中间包含的等待空闲时间不算)
|
|