鱼C论坛

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

关于课后作业的计时器问题

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

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

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

x
  1. import time as t

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

  11.     def __str__(self):
  12.         return self.prompt

  13.     __repr__ = __str__

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

  18.     def timing(self):
  19.         self.begin = self.default_timer()
  20.         for i in range(self.number):
  21.             self.func()
  22.         self.end = self.default_timer()
  23.         self.lasted = self.end - self.begin
  24.         self.prompt = '总共运行了 %.2f 秒' % self.lasted
  25.    

  26.     def set_timer(self,timer):
  27.         if timer == 'process_time':
  28.             self.default_timer = t.process_time
  29.         elif timer == 'perf_counter':
  30.             self.default_timer = t.perf_counter
  31.         else:
  32.             print('输入无效,请输入perf_counter 或 process_time')
复制代码


两个问题 第一个问题 def __init__(self,func,num= 1000000)
这里的func的作用是什么
包括func在后面for语句的循环中的作用也看不懂
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-7-15 15:59:38 | 显示全部楼层
还有最后一个方法 def set_timer(self)
这个函数的作用我也看不懂 希望大佬大神能指教
小甲鱼最新课程 -> 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.

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