鱼C论坛

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

课后作业44 关于时间函数的问题

[复制链接]
发表于 2019-7-15 15:39:30 | 显示全部楼层 |阅读模式

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

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

x
关于001题 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 __str__(self):
  10.         return self.prompt

  11.     __repr__ = __str__

  12.     def __add__(self,other):
  13.         self.result = self.lasted + other.lasted
  14.         self.prompt = '总共运行了 %f ' % self.result
  15.         return prompt

  16.     def start(self):
  17.         self.begin = self.default_timer()
  18.         self.prompt = '提示:请先调用 stop() 停止计时'
  19.         print('计时器开始...')

  20.     def stop(self):
  21.         if not self.begin:
  22.             print('提示:请先调用 start() 进行计时!')
  23.         else:
  24.             self.end = self.default_timer()
  25.             self._calc()
  26.             print('计时结束!')

  27.     def _calc(self):
  28.         self.lasted = self.end - self.begin
  29.         self.prompt = '总共运行了 %.2f 秒' % self.lasted

  30.         self.begin = 0
  31.         self.end = 0

  32.     [color=Red]def set_timer(self,timer):
  33.         if timer == 'process_time':
  34.             self.default_timer = t.process_time
  35.         elif timer == 'perf_counter':
  36.             self.default_timer = t.perf_counter
  37.         else:
  38.             print('输入无效,请输入perf_counter 或 process_time')[/color]
  39.             

复制代码


标红的方法中 有一个参数timer 这个是什么意思 这个方法我就不是很看得懂
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-7-15 15:40:35 | 显示全部楼层
不好意思 就是 def set_timer(self,timer)  这个timer参数是什么意思 包括这个方法 我也不是很懂是什么作用
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-16 21:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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