鱼C论坛

 找回密码
 立即注册
查看: 2705|回复: 4

[已解决]python 44讲 计时器 一个小问题

[复制链接]
发表于 2020-11-30 08:47:09 | 显示全部楼层 |阅读模式
3鱼币
  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.default_timer = t.perf_counter
  7.         self.func = func
  8.         self.number = number
  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.     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 = "总共运行了 %0.2f 秒" % self.lasted
  25.         
  26.     # 设置计时器(time.perf_counter() 或 time.process_time())
  27.     def set_timer(self, timer):
  28.         if timer == 'process_time':
  29.             self.default_timer = t.process_time
  30.         elif timer == 'perf_counter':
  31.             self.default_timer = t.perf_counter
  32.         else:
  33.             print("输入无效,请输入 perf_counter 或 process_time")
复制代码




运行:
  1. >>> ================================ RESTART ================================
  2. >>>
  3. >>> def test():
  4.         text = "I love FishC.com!"
  5.         char = 'o'
  6.         if char in text:
  7.                 pass

  8.         
  9. >>> t1 = MyTimer(test)
  10. >>> t1.timing()
  11. >>> t1
  12. 总共运行了 0.27 秒
  13. >>> t2 = MyTimer(test, 100000000)
  14. >>> t2.timing()
  15. >>> t2
  16. 总共运行了 25.92 秒
  17. >>> t1 + t2
  18. '总共运行了 26.19 秒'
复制代码


self.func() 在这有什么意义?
最佳答案
2020-11-30 08:47:10
1、t1 = MyTimer(test) 的时候,传进了一个函数对象test

2、实例化类对象赋值给t1的时候,首先执行的是类初始化函数
class MyTimer:
    def __init__(self, func, number=1000000):
        self.prompt = "未开始计时!"
        self.lasted = 0.0
        self.default_timer = t.perf_counter
        self.func = func            # 这里把test这个函数对象赋值给了self.func

3、执行t1.timing()的时候:
    def timing(self):
        self.begin = self.default_timer()
        for i in range(self.number):
            self.func()    # 这里执行的就是test了
        self.end = self.default_timer()
        self.lasted = self.end - self.begin
        self.prompt = "总共运行了 %0.2f 秒" % self.lasted

最佳答案

查看完整内容

1、t1 = MyTimer(test) 的时候,传进了一个函数对象test 2、实例化类对象赋值给t1的时候,首先执行的是类初始化函数 class MyTimer: def __init__(self, func, number=1000000): self.prompt = "未开始计时!" self.lasted = 0.0 self.default_timer = t.perf_counter self.func = func # 这里把test这个函数对象赋值给了self.func 3、执行t1.timing()的时候: d ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-30 08:47:10 | 显示全部楼层    本楼为最佳答案   
1、t1 = MyTimer(test) 的时候,传进了一个函数对象test

2、实例化类对象赋值给t1的时候,首先执行的是类初始化函数
class MyTimer:
    def __init__(self, func, number=1000000):
        self.prompt = "未开始计时!"
        self.lasted = 0.0
        self.default_timer = t.perf_counter
        self.func = func            # 这里把test这个函数对象赋值给了self.func

3、执行t1.timing()的时候:
    def timing(self):
        self.begin = self.default_timer()
        for i in range(self.number):
            self.func()    # 这里执行的就是test了
        self.end = self.default_timer()
        self.lasted = self.end - self.begin
        self.prompt = "总共运行了 %0.2f 秒" % self.lasted
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-11-30 09:22:52 | 显示全部楼层
笨鸟学飞 发表于 2020-11-30 09:15
1、t1 = MyTimer(test) 的时候,传进了一个函数对象test

2、实例化类对象赋值给t1的时候,首先执行的是 ...

那 test 这个方法怎么写都行咯? 可我感觉会拖延一些时间
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-30 09:28:47 | 显示全部楼层
wuyanzulqq 发表于 2020-11-30 09:22
那 test 这个方法怎么写都行咯? 可我感觉会拖延一些时间

1、test方法确实可以任意写的,这个例子其实就是告诉你类在编程中的使用,大体的方法都有了,看你在具体编程过程中怎样去灵活运用咯。。。你问的这里的就是把外部函数,和类怎么串联起来用

2、拖时间是必然的啊,代码的执行都是要时间的,只是不同语言效率不同,但是都是要时间的,你可以在代码执行前获取时间,执行后获取时间,就知道了(精度小了显示不出时间消耗)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-11-30 09:51:56 | 显示全部楼层
笨鸟学飞 发表于 2020-11-30 09:28
1、test方法确实可以任意写的,这个例子其实就是告诉你类在编程中的使用,大体的方法都有了,看你在具体 ...

感谢解答
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-30 13:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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