鱼C论坛

 找回密码
 立即注册
查看: 2353|回复: 3

[已解决]Python计时器问题

[复制链接]
发表于 2017-10-10 20:21:59 | 显示全部楼层 |阅读模式

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

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

x
在零基础44讲课后题中,小甲鱼让利用perf_counter()来进行计时,我的代码和小甲鱼的有一处不一样,结果我的计时就全部是0.0,他的就可以正常计时,谁能告诉我为什么??(注:不一样的地方小甲鱼写的是:self.method = t.perf_counter,后面调用的时候是self.begin = self.method(),我的是self.method = t.perf_counter(),调用时是self.begin = self.method)
  1. import time as t
  2. class Mytime():
  3.     def __init__(self):
  4.         self.begin = 0
  5.         self.end = 0
  6.         self.lasted = 0.0
  7.         self.pro = '未开始计时!'
  8.         self.method = t.perf_counter()
  9.     def __str__(self):
  10.         return self.pro
  11.    
  12.     __repr__ = __str__

  13.     def set_timer(self,method = t.perf_counter()):
  14.         self.method = method
  15.         print('您当前设置的计时器为:')
  16.         
  17.     #计时开始
  18.     def start(self):
  19.             self.begin = self.method
  20.             self.pro ='提示,请用stop()停止计时'
  21.             print('计时开始了~')
  22.     #计时结束
  23.     def stop(self):
  24.         if not self.begin:
  25.             print('请先运行start()方法')
  26.         else:
  27.             self.end = self.method
  28.             self.cal()
  29.             print('计时结束')
  30.     #计算时长
  31.     def cal(self):
  32.         self.lasted = self.end - self.begin
  33.         self.pro = '总共运行了%0.2f秒'% self.lasted
  34.         #检查时间相减是否有负数出现

  35.     #重新归零
  36.         self.begin = 0
  37.         self.end = 0

复制代码
最佳答案
2017-10-10 21:03:11
是这样的,如果将self.method = t.perf_counter()那么此时的self.method已经被赋了值,假设
  1. >>> a = time.perf_counter()
  2. >>> a
  3. 1299.2883861479334
复制代码

那么你后来的self.begin=self.method的时候,self.method已经是一个固定的数值了
self.end=self.method也是如此
但是,你将self.method=t.perf_counter的话,情况就不同了
  1. >>> import time
  2. >>> a = time.perf_counter
  3. >>> a
  4. <built-in function perf_counter>
  5. >>> a()
  6. 8.553087087105066e-07
复制代码

此时self.method是一perf_counter函数

还有你上面的set_time函数,里面的methd也需要更改,更改为method = t.perf_counter
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-10-10 21:03:11 | 显示全部楼层    本楼为最佳答案   
是这样的,如果将self.method = t.perf_counter()那么此时的self.method已经被赋了值,假设
  1. >>> a = time.perf_counter()
  2. >>> a
  3. 1299.2883861479334
复制代码

那么你后来的self.begin=self.method的时候,self.method已经是一个固定的数值了
self.end=self.method也是如此
但是,你将self.method=t.perf_counter的话,情况就不同了
  1. >>> import time
  2. >>> a = time.perf_counter
  3. >>> a
  4. <built-in function perf_counter>
  5. >>> a()
  6. 8.553087087105066e-07
复制代码

此时self.method是一perf_counter函数

还有你上面的set_time函数,里面的methd也需要更改,更改为method = t.perf_counter
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-10 22:25:28 | 显示全部楼层
Teagle 发表于 2017-10-10 21:03
是这样的,如果将self.method = t.perf_counter()那么此时的self.method已经被赋了值,假设

那么你后来 ...

哇原来是这样,好棒的解答!!多谢么么哒
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-10 22:35:38 | 显示全部楼层
Coolsize 发表于 2017-10-10 22:25
哇原来是这样,好棒的解答!!多谢么么哒

客气啦,加油奥
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-18 21:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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