鱼C论坛

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

为什么time.clock和time.time的计时时间不同

[复制链接]
发表于 2019-4-27 16:53:01 | 显示全部楼层 |阅读模式

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

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

x
以数字求和为例,发现time会返回一个较少的值,而clock会较大,为什么会这样

  1. import time
  2. '''
  3. def foo(x,y):
  4.     tt = time.clock()
  5.     s = 0
  6.     for i in range(x,y):
  7.         s += i
  8.     print("Time used:{} sec".format(time.clock()))
  9.     return s

  10. print(foo(1,1000000))

  11. '''
  12. '''
  13. def foo(x,y):
  14.     tt = time.time()
  15.     s = 0
  16.     for i in range(x,y):
  17.         s += i
  18.     print('Time used: {} sec'.format(time.time()-tt))
  19.     return s

  20. print(foo(1,1000000))
  21. '''
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-27 17:29:34 | 显示全部楼层
两次运行结果:
  1. >>>
  2. ============== RESTART: C:\Users\Administrator\Desktop\test.pyw ==============

  3. Warning (from warnings module):
  4.   File "C:\Users\Administrator\Desktop\test.pyw", line 4
  5.     tt = time.clock()
  6. DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead

  7. Warning (from warnings module):
  8.   File "C:\Users\Administrator\Desktop\test.pyw", line 8
  9.     print("Time used:{} sec".format(time.clock()))
  10. DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
  11. Time used:1.108299428 sec
  12. 499999500000
复制代码
  1. >>>
  2. ============== RESTART: C:\Users\Administrator\Desktop\test.pyw ==============
  3. Time used: 0.23301315307617188 sec
  4. 499999500000
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-4-27 20:31:56 | 显示全部楼层
TCY 发表于 2019-4-27 17:29
两次运行结果:

所以为什么会这样呀。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-27 20:49:52 | 显示全部楼层
我测试差不多;
  1. ========================== RESTART: D:\py\test2.py ==========================
  2. Time used: 0.16892194747924805 sec
  3. time 499999500000
  4. >>>
  5. ========================== RESTART: D:\py\test2.py ==========================
  6. Time used:0.1626465 sec
  7. clock 499999500000
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-1 11:22:39 | 显示全部楼层
time.clock()

用以浮点数计算的秒数返回当前的 CPU 时间。用来衡量不同程序的耗时,比 time.time() 更有用。

Python 3.3 以后不被推荐,由于该方法依赖操作系统,建议使用 perf_counter() 或 process_time() 代替(一个返回系统运行时间,一个返回进程运行时间,请按照实际需求选择)
time.time()

返回当前时间的时间戳(1970 纪元年后经过的浮点秒数)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-15 12:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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