2016-07-22 67 views
3

我想知道为什么python 2.7使用gettimeofday()运行时time.time()但尚未在python 3.4它不?python time.time()python 2 vs 3的区别

运行strace的,当它看来,它可查询的/ etc /本地时间

+0

做u面对任何问题呢? –

+0

不同的实现有什么问题吗?添加了各种新的时钟类型后,Python 3'time'模块被彻底检查了。 –

+0

是使用虚拟时间内核https://github.com/littlepretty/VirtualTimeKernel 哪些依赖于GToD – channon

回答

3

的Python 3将使用gettimeofday()当检测你的系统来支持这种在编译时。但是,在POSIX系统上,只有在clock_gettime(CLOCK_REALTIME)不可用时才会使用该功能;根据POSIX 2008 standard,后者是优选的,因为gettimeofday()被认为是过时的。

在运行时,你可以查询什么的Python认为你的系统可以通过使用time.get_clock_info() function,它返回一个namedtuple实例与implementation场在编译时支持:

实施:底层的名字C函数用来获取时钟值

在我的OSX 10.11体系,为'time'时钟,产生gettimeofday()

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

您可以阅读pygettimeofday() C implementation以查看可以使用哪些实现;例如,在Windows上使用GetSystemTimeAsFileTime()

+0

感谢您的信息。我会试着强制它使用GToD作为它的时钟机制。 – channon

+0

@channon:不知道为什么你需要我见[VirtualTimeKernel支持'clock_gettime()'](https://github.com/littlepretty/VirtualTimeKernel/blob/7d3b11be676df5e828f9c52b34990ebc55330e72/arch/x86/vdso/vclock_gettime.c )。 –

+0

,因为虚拟时间只影响GToD它在该函数的代码中 – channon