【问题标题】:Why does Python's time.time() return a wrong result?为什么 Python 的 time.time() 返回错误的结果?
【发布时间】:2015-10-22 05:57:52
【问题描述】:

我不是问time.time()time.clock() 之间的区别,我问的是为什么返回的结果完全错误。

它说已经过了 37 秒,而实际上大约是 5 秒...这是我的操作系统时钟的问题吗?我的python安装?我四处搜寻,但找不到有相同问题的人。

这是我的代码:

t0 = time.time()
weightMatrix.MultiprocessMatrix(sigmaI, sigmaX)
t1 = time.time() - t0
print('Parallel took {} seconds'.format(t1))

t0 = time.time()
weightMatrix.CreateMatrix(sigmaI, sigmaX)
t2 = time.time() - t0
print('Serial took {} seconds'.format(t2))
print('Parallel time was {}% faster than serial time!'.format((t2/t1)*100))

第二个,t2 看起来是准确的,但第一个报告的数字比它应该的要大得多。

Edit2:我只是在想,time.time() 据说只测量时钟时间。但是time.clock() 测量处理器时间。我正在使用time(),所以这应该是一个问题,但是......我正在尝试比较函数的串行方法和并行方法。如果一个如果串行运行多个进程,它实际上会比一个核心测量更多的处理时间?但同样,使用time() 并不重要。我不知道,因为第二个(串行函数)似乎可以正确测量时间。

【问题讨论】:

  • 你用什么操作系统? (我认为 time.time 和 time.clock 是依赖于操作系统的)
  • 很难对未显示的代码进行有用的评论。
  • 也可能是延迟问题
  • @NightShadeQueen windows 8.1
  • 您可能会遇到Microsoft Minute

标签: python windows time windows-8.1 timing


【解决方案1】:

根据帮助文档,time.clock():

关于模块时间内置函数时钟的帮助:

时钟(...) 时钟() -> 浮点数

Return the CPU time or real time since the start of the process or since
the first call to clock().  This has as much precision as the system
records.

所以我不知道你在没有代码的情况下要做什么,但我猜它与 time.clock() 正在做什么与你想要完成的事情有关?

【讨论】:

  • 我只是想给两个单独的函数计时以进行比较。查看我编辑的帖子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-05
  • 2016-05-28
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多