鱼C论坛

 找回密码
 立即注册
查看: 1140|回复: 1

[已解决]关于第44课后作业 process_time 跟 perf_counter

[复制链接]
发表于 2018-9-27 16:09:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
问题1:没看懂小甲鱼给出的答案的作用
问题2:process_time 跟 perf_counter到底怎么用

  1. import time as t

  2. class MyTimer():
  3.     def __init__(self):
  4.         self.prompt="未开始计时"
  5.         self.lasted=0.0
  6.         self.begin=0
  7.         self.end=0
  8.         self.default_timer=t.perf_counter
  9.     def __repr__(self):
  10.         return self.prompt

  11.     def __add__(self,other):
  12.         result=self.lasted+other.lasted
  13.         prompt="总共运行了%0.2f秒"%result
  14.         return prompt
  15.    
  16.     # 开始计时
  17.     def start(self):
  18.         self.begin=self.default_timer()
  19.         self.prompt="提示:请先调用stop()停止计时!"
  20.         print("开始计时...")
  21.     # 停止计时
  22.     def stop(self):
  23.         if not self.begin:
  24.             print("提示:请先调用start()进行计时")
  25.         else:
  26.             self.end=self.default_timer()
  27.             self._calc()
  28.             print("计时停止!")
  29.     #内部方法,计算运行时间
  30.     def _calc(self):
  31.         self.lasted=self.end-self.begin
  32.         self.prompt="总共运行了%0.2f秒"%self.lasted
  33.         # 为下一轮计时初始化变量
  34.         self.begin=0
  35.         self.end=0
  36.     def set_timer(self,timer):
  37.         if timer=='process_time':
  38.             self.default_timer=t.process_time
  39.         elif timer == 'perf_counter':
  40.             self.default_timer=t.perf_counter
  41.         else:
  42.             print("输入无效,请输入process_time或perf_counter")
复制代码
最佳答案
2018-9-27 16:25:27
2个都是连续运行差值给出时间,单位是秒
perf_counter是程序运行时间
process_time是CPU有效运行时间(中间包含的等待空闲时间不算)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-27 16:25:27 | 显示全部楼层    本楼为最佳答案   
2个都是连续运行差值给出时间,单位是秒
perf_counter是程序运行时间
process_time是CPU有效运行时间(中间包含的等待空闲时间不算)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-14 02:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表