【问题标题】:python time.time() differences in python 2 vs 3python time.time() 在 python 2 与 3 中的差异
【发布时间】:2016-07-22 19:09:57
【问题描述】:

我想知道为什么python 2.7 在运行time.time() 时使用gettimeofday() 但在python 3.4 中却没有?

运行strace时出现可能查询/etc/localtime

【问题讨论】:

标签: python python-2.7 python-3.x python-internals gettimeofday


【解决方案1】:

当您的系统在编译时检测到支持此功能时,Python 3 将使用gettimeofday()。但是,在 POSIX 系统上,它只会在 clock_gettime(CLOCK_REALTIME) 不可用时使用它;根据POSIX 2008 standard,后者是首选,因为gettimeofday() 被认为已过时。

在运行时,您可以使用 time.get_clock_info() function 查询 Python 认为您的系统在编译时可以支持什么,它返回一个带有 implementation 字段的 namedtuple 实例:

实现:用于获取时钟值的底层C函数的名称

在我的 OSX 10.11 系统上,对于 'time' 时钟,产生 gettimeofday():

>>> time.get_clock_info('time').implementation
'gettimeofday()'

您可以通读pygettimeofday() C implementation 以了解可以使用哪些实现;例如在 Windows 上使用 GetSystemTimeAsFileTime()。

【讨论】:

  • 感谢您的信息。我会尝试强制它使用 GToD 作为其时钟机制。
  • @channon:不知道你为什么需要我看到VirtualTimeKernel supports clock_gettime()。
  • 因为虚拟时间只影响 GToD,它在该函数的代码中
猜你喜欢
  • 1970-01-01
  • 2017-06-03
  • 2011-11-07
  • 2020-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多