鱼C论坛

 找回密码
 立即注册
楼主: 小甲鱼

[扩展阅读] timeit 模块详解(准确测量小段代码的执行时间)

  [复制链接]
发表于 2018-9-25 10:52:05 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-25 22:51:26 | 显示全部楼层
支持,支持
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-26 10:11:46 | 显示全部楼层
666
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-26 19:41:20 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-27 15:58:33 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-27 16:13:05 | 显示全部楼层
我看看!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-27 21:33:46 From FishC Mobile | 显示全部楼层
学习学习
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-29 13:46:06 | 显示全部楼层
  1. >>> import timeit
  2. >>> import timeit
  3. >>> timeit.timeit('"-".join(str(n) for n in range(100))',number=10000)
  4. 0.22145586600001366
  5. >>> timeit.timeit('"-".join(str(n) for n in range(100))',number=100000)
  6. 2.19172915499999
  7. >>> timeit.timeit('a = "-".join(str(n) for n in range(100))',number=10000)
  8. 0.22280868299998247
  9. >>> timeit.timeit('a = "-".join(str(n) for n in range(100));return a',number=10000)
  10. >>> timeit.timeit('a = "-".join(str(n) for n in range(100))',repeat=3,number=10000)
  11. Traceback (most recent call last):
  12.   File "<pyshell#9>", line 1, in <module>
  13.     timeit.timeit('a = "-".join(str(n) for n in range(100))',repeat=3,number=10000)
  14. TypeError: timeit() got an unexpected keyword argument 'repeat'
  15. >>> timeit.repeat('a = "-".join(str(n) for n in range(100))',repeat=3,number=10000)
  16. [0.21651620500006175, 0.22220623099997283, 0.21522767299995849]
  17. >>> timeit.repeat('a = "-".join(str(n) for n in range(100))',timer = <process_time()>,repeat=3,number=10000)
  18. SyntaxError: invalid syntax
  19. >>> timeit.repeat('a = "-".join(str(n) for n in range(100))',timer =<process_time()>,repeat=3,number=10000)
  20. SyntaxError: invalid syntax
  21. >>> timeit.repeat('a = "-".join(str(n) for n in range(100))',timer = process_time(),repeat=3,number=10000)
  22. Traceback (most recent call last):
  23.   File "<pyshell#13>", line 1, in <module>
  24.     timeit.repeat('a = "-".join(str(n) for n in range(100))',timer = process_time(),repeat=3,number=10000)
  25. NameError: name 'process_time' is not defined
  26. >>> timeit.Timer('for i in range(10): oct(i)', 'gc.enable()').timeit()
  27. 1.1840565520001292
  28. >>> timeit.Timer('for i in range(10): oct(i)', 'gc.enable()')
  29. <timeit.Timer object at 0x000002311996BFD0>
  30. >>> a=timeit.Timer('for i in range(10): oct(i)', 'gc.enable()')
  31. >>> a
  32. <timeit.Timer object at 0x00000231199A5EF0>
  33. >>> a.timeit()
  34. 1.2091490960001465
  35. >>> a.repeat()
  36. [1.1775791850000132, 1.2054382349999742, 1.1808787390000361, 1.1793837000000167, 1.1820034329998634]
  37. >>> s = '''\
  38. try:
  39.         str.__bool__
  40. except AttributeError:
  41.         pass
  42. '''
  43. >>> timeit.timeit(stmt=s, number = 100000)
  44. 0.041749605999939376
  45. >>> s = 'if hasattr(str,"__bool__"):pass'
  46. >>> timeit.timeit(stmt=s, number = 100000)
  47. 0.027976809999927355
  48. >>> s = '''\
  49. try:
  50.         int.__bool__
  51. except AttributeError:
  52.         pass
  53. '''
  54. >>> timeit.timeit(stmt=s, number = 100000)
  55. 0.004601171999865983
  56. >>> s = 'if hasattr(int,"__bool__"):pass'
  57. >>> timeit.timeit(stmt=s, number = 100000)
  58. 0.006767500000023574
复制代码


timeit,repeat方法中的timer的使用,暂时还未掌握
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-1 09:37:08 | 显示全部楼层
如何读取文本中包含的时间
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-1 21:46:26 | 显示全部楼层
支持小甲鱼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-2 17:34:23 | 显示全部楼层
kk
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-2 21:46:02 | 显示全部楼层
支持小甲鱼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-3 09:42:49 | 显示全部楼层
支持小甲鱼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-3 13:57:14 | 显示全部楼层
看看源代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-4 16:28:32 | 显示全部楼层
666
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-5 07:55:43 | 显示全部楼层
支持楼主!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-5 14:19:25 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-6 12:10:38 | 显示全部楼层
小甲鱼你写错别字了,是大有裨益哈哈哈。。.
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-8 10:23:06 | 显示全部楼层
强路费灰飞烟灭
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-8 14:56:57 | 显示全部楼层
支持楼主
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-14 23:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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