鱼C论坛

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

【问题求助】第四十四讲最后一题的问题!!

[复制链接]
发表于 2019-8-2 16:47:03 | 显示全部楼层 |阅读模式

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

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

x
import time as t

class MyTimer():

    def __init__(self,func,count = 1):
       self.temp = 0
       self.prompt = '总共运行了0秒'
       self.cou = count
       self.func = func
      
    def __str__(self):
        return self.prompt

    __repr__ = __str__

    def timing(self):
        while self.cou:
            self.bagin = t.perf_counter()
            self.func()
            self.end = t.perf_counter()
            self.temp += self.end - self.bagin
            self.cou -= 1

        self.prompt = '总共运行了%0.2f秒'%self.temp

    def __add__(self,other):
        self.atemp = self.temp + other.temp
        self.aprompt = '总共运行了%0.2f秒'%self.atemp
        return self.aprompt

这是我写的代码,按照小甲鱼方法的运行
>>> ================================ RESTART ================================
>>>
>>> def test():
        text = "I love FishC.com!"
        char = 'o'
        if char in text:
                pass

        
>>> t1 = MyTimer(test)
>>> t1.timing()
>>> t1
总共运行了 0.00秒

请问为什么运行结果是0秒?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-2 16:51:50 | 显示全部楼层
那个小函数 Python 不过一毫秒就执行完了,建议你搞些 for 循环测试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-17 10:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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