简体   繁体   English

python time.time()python 2 vs 3的差异

[英]python time.time() differences in python 2 vs 3

I am wondering why python 2.7 uses gettimeofday() when running time.time() but yet in python 3.4 it does not? 我想知道为什么python 2.7在运行time.time()时使用gettimeofday() time.time()但是在python 3.4却没有?

It appears when running strace that it may be querying /etc/localtime 它在运行strace时出现,它可能正在查询/ etc / localtime

Python 3 will use gettimeofday() when your system has been detected to support this at compile time . 当您的系统在编译时被检测到支持时,Python 3将使用gettimeofday() However, on POSIX systems it'll only use that if clock_gettime(CLOCK_REALTIME) is not available instead; 但是,在POSIX系统上,只有当clock_gettime(CLOCK_REALTIME)不可用时才会使用它; according to the POSIX 2008 standard the latter is preferred as gettimeofday() is considered obsolete. 根据POSIX 2008标准 ,后者是首选,因为gettimeofday()被认为是过时的。

At runtime, you can query what Python thought your system could support at compile time by using the time.get_clock_info() function , which returns a namedtuple instance with a implementation field: 在运行时,你可以查询什么的Python认为你的系统可以通过在编译时支持time.get_clock_info()函数 ,它返回一个namedtuple实例与implementation领域:

implementation : The name of the underlying C function used to get the clock value implementation :用于获取时钟值的基础C函数的名称

On my OSX 10.11 system, for the 'time' clock, that produces gettimeofday() : 在我的OSX 10.11系统上,对于'time'时钟,产生gettimeofday()

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

You can read through the pygettimeofday() C implementation to see what implementations may be used; 您可以通读pygettimeofday() C实现来查看可以使用的实现; on Windows GetSystemTimeAsFileTime() is used for example. 例如,在Windows上使用GetSystemTimeAsFileTime()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM