鱼C论坛

 找回密码
 立即注册
查看: 1738|回复: 0

[学习笔记] for比while快吗?

[复制链接]
发表于 2020-9-7 14:34:46 | 显示全部楼层 |阅读模式

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

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

x
在学time模块
1、用while时
  1. import time as t

  2. class Thirdtimer():
  3.     def __init__(self,func,times = 100000):
  4.         self.default_timer = t.perf_counter
  5.         self.prompt = "未开始计时!"
  6.         self.lasted = 0.0
  7.         self.func = func
  8.         self.times = times
  9.    
  10.     def __str__(self):
  11.         return self.prompt
  12.     __repr__ = __str__
  13.     #多次计时
  14.     def __add__(self,other):
  15.         result = self.lasted + other.lasted
  16.         prompt = "总共运行了 %0.2f 秒" % result
  17.         return prompt
  18.     #启动计时器
  19.     def timing(self):
  20.         self.begin = self.default_timer()
  21.         while self.times > 0:
  22.             self.times -= 1
  23.             self.func()
  24.         self.end = self.default_timer()
  25.         self.lasted = self.end - self.begin
  26.         self.prompt = "总共运行了 %0.2f 秒" % self.lasted
  27.         self.begin = 0
  28.         self.end = 0
  29.     #设置默认计时器
  30.     def set_timer(self,timer):
  31.         if timer == 'process_timer':
  32.             self.default_timer = t.process_time
  33.         elif timer == 'perf_counter':
  34.             self.default_timer = t.perf_counter
  35.         else:
  36.             print('输入无效,请输入perf_counter或process_timer')
  37. def test():
  38.     text = "I love Fishc.com"
  39.     char = 'o'
  40.     if char in text:
  41.         pass
复制代码



2、用for时

  1. import time as t

  2. class Thirdtimer():
  3.     def __init__(self,func,times = 100000):
  4.         self.default_timer = t.perf_counter
  5.         self.prompt = "未开始计时!"
  6.         self.lasted = 0.0
  7.         self.func = func
  8.         self.times = times
  9.    
  10.     def __str__(self):
  11.         return self.prompt
  12.     __repr__ = __str__
  13.     #多次计时
  14.     def __add__(self,other):
  15.         result = self.lasted + other.lasted
  16.         prompt = "总共运行了 %0.2f 秒" % result
  17.         return prompt
  18.     #启动计时器
  19.     def timing(self):
  20.         self.begin = self.default_timer()
  21.         for i in range(self.times):
  22.             self.func()
  23.         self.end = self.default_timer()
  24.         self.lasted = self.end - self.begin
  25.         self.prompt = "总共运行了 %0.2f 秒" % self.lasted
  26.         self.begin = 0
  27.         self.end = 0
  28.     #设置默认计时器
  29.     def set_timer(self,timer):
  30.         if timer == 'process_timer':
  31.             self.default_timer = t.process_time
  32.         elif timer == 'perf_counter':
  33.             self.default_timer = t.perf_counter
  34.         else:
  35.             print('输入无效,请输入perf_counter或process_timer')
  36. def test():
  37.     text = "I love Fishc.com"
  38.     char = 'o'
  39.     if char in text:
  40.         pass
复制代码
idle中,31.39是while循环,13.79是for循环,这是不是说明for比while快,因为while多了判断吗?哈哈哈,吹吹水,如果有大佬懂原理跟我说一声
  1. >>> t1 = Thirdtimer(test,100000000)
  2. >>> t1.timing()
  3. >>> t1
  4. 总共运行了 31.39 秒
  5. >>>
  6. ===================== RESTART: C:\Users\86181\Desktop\44.py ====================
  7. >>> t1 = Thirdtimer(test,100000000)
  8. >>> t1.timing()
  9. >>> t1
  10. 总共运行了 13.79 秒
复制代码



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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-28 02:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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