鱼C论坛

 找回密码
 立即注册
查看: 2666|回复: 2

[已解决]关于process_time的用法?

[复制链接]
发表于 2017-4-6 21:57:36 | 显示全部楼层 |阅读模式

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

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

x
在小甲鱼老师http://bbs.fishc.com/thread-55756-1-1.html这一讲作业中
  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.    
  10.     def __str__(self):
  11.         return self.prompt

  12.     __repr__ = __str__

  13.     def __add__(self, other):
  14.         result = self.lasted + other.lasted
  15.         prompt = "总共运行了 %0.2f 秒" % result
  16.         return prompt
  17.    
  18.     # 开始计时
  19.     def start(self):
  20.         self.begin = self.default_timer()
  21.         self.prompt = "提示:请先调用 stop() 停止计时!"
  22.         print("计时开始...")

  23.     # 停止计时
  24.     def stop(self):
  25.         if not self.begin:
  26.             print("提示:请先调用 start() 进行计时!")
  27.         else:
  28.             self.end = self.default_timer()
  29.             self._calc()
  30.             print("计时结束!")

  31.     # 内部方法,计算运行时间
  32.     def _calc(self):
  33.         self.lasted = self.end - self.begin
  34.         self.prompt = "总共运行了 %0.2f 秒" % self.lasted
  35.         
  36.         # 为下一轮计时初始化变量
  37.         self.begin = 0
  38.         self.end = 0

  39.     # 设置计时器(time.perf_counter() 或 time.process_time())
  40.     def set_timer(self, timer):
  41.         if timer == 'process_time':
  42.             self.default_timer = t.process_time
  43.         elif timer == 'perf_counter':
  44.             self.default_timer = t.perf_counter
  45.         else:
  46.             print("输入无效,请输入 perf_counter 或 process_time")
复制代码


有一个涉及到process_time的参数,不太清楚这个是用来干什么的?我执行了
  1. t1.set_timer('process_time')
复制代码


之后发现t1在执行完start和stop方法,我再输入t1 弹出时间为0
最佳答案
2017-4-6 22:41:47
http://stackoverflow.com/questions/25785243/understanding-time-perf-counter-and-time-process-time
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-6 22:41:47 | 显示全部楼层    本楼为最佳答案   
http://stackoverflow.com/questions/25785243/understanding-time-perf-counter-and-time-process-time
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-7 17:43:39 | 显示全部楼层
帮二楼补充下,二楼的答案说的很详细的
process_time是主要计算的是在CPU上运行的时间,而我们写的小程序很大部分不会在CPU上处理很长时间,你的精度比较小,所以打印出来是0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-9 13:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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