time.time_ns() 在 macOS 上没有正确返回纳秒? [英] time.time_ns() is not returing nanoseconds correctly on macOS?

查看:35
本文介绍了time.time_ns() 在 macOS 上没有正确返回纳秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 Python 3.7 开始,我们有了支持纳秒分辨率的新时间函数.但是,我不确定 time.time_ns() 应该如何工作.

看下面的例子:

<预><代码>>>>对于范围内的 n(10):... time.sleep(random.random())...打印((time.time(), time.time_ns(), time.monotonic_ns()))...(1545306865.8667252, 1545306865866727000, 439497985080)(1545306866.084973, 1545306866084974000, 439716229679)(1545306866.2972622, 1545306866297264000, 439928562751)(1545306866.635714, 1545306866635716000, 440267014751)(1545306866.745001, 1545306866745003000, 440376301646)(1545306867.212074, 1545306867212076000, 440843415181)(1545306867.7111092, 1545306867711111000, 441342449470)(1545306867.792372, 1545306867792374000, 441423713091)(1545306867.821886, 1545306867821887000, 441453223973)(1545306868.127483, 1545306868127485000, 441758824065)

如您所见,time.time_ns() 确实将时间作为具有纳秒精度的整数返回,但最后一位数字始终是 000.这不应该是这种情况.是错误还是我遗漏了什么?

解决方案

这是关于精度的.python 中的每个时钟都有相应的精度,这解释了您正在经历的差异.让我们看看 Macbook Pro 2018 和 MacOS Mojave 的时钟细节.Python3.7 是通过 brew 安装的:

在 [41]: time.perf_counter_ns()出[41]:10464788941125在 [42]: time.process_time_ns()出[42]:22502272000在 [43] 中:time.time_ns()出[43]:1545312118561931000在 [44]: time.monotonic_ns()出[44]:10477720411470在 [45] 中:time.get_clock_info('perf_counter')Out[45]: 命名空间(adjustable=False, implementation='mach_absolute_time()', monotonic=True,resolution=1e-09)在 [46]: time.get_clock_info('process_time')出[46]:命名空间(可调整=假,实现=clock_gettime(CLOCK_PROCESS_CPUTIME_ID)",单调=真,分辨率=1.0000000000000002e-06)在 [47]: time.get_clock_info('time')出[47]:命名空间(可调整=真,实现=clock_gettime(CLOCK_REALTIME)",单调=假,分辨率=1.0000000000000002e-06)在 [48]: time.get_clock_info('monotonic')Out[48]: namespace(adjustable=False, implementation='mach_absolute_time()', monotonic=True,resolution=1e-09)

请注意实现解析.以下是相同的信息,但来自运行在 Ubuntu 服务器上的虚拟机:

<预><代码>>>>time.perf_counter_ns()4094438601446186>>>time.process_time_ns()35344006>>>时间.time_ns()1545312252720125938>>>time.monotonic_ns()4094449881239590>>>time.get_clock_info('perf_counter')命名空间(可调整=假,实现=时钟获取时间(时钟_MONOTONIC)",单调=真,分辨率=1e-09)>>>time.get_clock_info('时间')命名空间(可调=真,实现=时钟获取时间(时钟实时),单调=假,分辨率=1e-09)>>>time.get_clock_info('process_time')命名空间(可调整=假,实现=clock_gettime(CLOCK_PROCESS_CPUTIME_ID)",单调=真,分辨率=1e-09)>>>time.get_clock_info('单调')命名空间(可调整=假,实现=时钟获取时间(时钟_MONOTONIC)",单调=真,分辨率=1e-09)

如您所见,每个时钟都有不同的实现和精度,这取决于平台.在 MacOS 上,process_timetime 时钟的时钟精度设置为 1e-06,即微秒.这解释了差异.

您也可以使用 time.clock_getres 来获取精度:

在[51]中:time.clock_getres(time.CLOCK_REALTIME)出[51]:1.0000000000000002e-06

进一步阅读:

Starting from Python 3.7 we have new time functions supporting nanosecond resolution. However, I am not sure how time.time_ns() is supposed to work.

Look at the following example:

>>> for n in range(10):
...     time.sleep(random.random())
...     print((time.time(), time.time_ns(), time.monotonic_ns()))
...
(1545306865.8667252, 1545306865866727000, 439497985080)
(1545306866.084973, 1545306866084974000, 439716229679)
(1545306866.2972622, 1545306866297264000, 439928562751)
(1545306866.635714, 1545306866635716000, 440267014751)
(1545306866.745001, 1545306866745003000, 440376301646)
(1545306867.212074, 1545306867212076000, 440843415181)
(1545306867.7111092, 1545306867711111000, 441342449470)
(1545306867.792372, 1545306867792374000, 441423713091)
(1545306867.821886, 1545306867821887000, 441453223973)
(1545306868.127483, 1545306868127485000, 441758824065)

As you can see, time.time_ns() does return time as an integer with nanosecond precision, but the last digits are always 000. Which should not be the case. Is it a bug or am I missing something?

解决方案

It's about precision. Each clock in python has a corresponding precision which explains the difference that you are experiencing. Let's see the clock details from a Macbook Pro 2018 with MacOS Mojave. Python3.7 is installed via brew:

In [41]: time.perf_counter_ns()
Out[41]: 10464788941125

In [42]: time.process_time_ns()
Out[42]: 22502272000

In [43]: time.time_ns()
Out[43]: 1545312118561931000

In [44]: time.monotonic_ns()
Out[44]: 10477720411470

In [45]: time.get_clock_info('perf_counter')
Out[45]: namespace(adjustable=False, implementation='mach_absolute_time()', monotonic=True, resolution=1e-09)

In [46]: time.get_clock_info('process_time')
Out[46]: namespace(adjustable=False, implementation='clock_gettime(CLOCK_PROCESS_CPUTIME_ID)', monotonic=True, resolution=1.0000000000000002e-06)

In [47]: time.get_clock_info('time')
Out[47]: namespace(adjustable=True, implementation='clock_gettime(CLOCK_REALTIME)', monotonic=False, resolution=1.0000000000000002e-06)

In [48]: time.get_clock_info('monotonic')
Out[48]: namespace(adjustable=False, implementation='mach_absolute_time()', monotonic=True, resolution=1e-09)

Please pay attention to implementation and resolution. Here are the same info but from a VM running on an Ubuntu server:

>>> time.perf_counter_ns()
4094438601446186

>>> time.process_time_ns()
35344006

>>> time.time_ns()
1545312252720125938

>>> time.monotonic_ns()
4094449881239590

>>> time.get_clock_info('perf_counter')
namespace(adjustable=False, implementation='clock_gettime(CLOCK_MONOTONIC)', monotonic=True, resolution=1e-09)

>>> time.get_clock_info('time')
namespace(adjustable=True, implementation='clock_gettime(CLOCK_REALTIME)', monotonic=False, resolution=1e-09)

>>> time.get_clock_info('process_time')
namespace(adjustable=False, implementation='clock_gettime(CLOCK_PROCESS_CPUTIME_ID)', monotonic=True, resolution=1e-09)

>>> time.get_clock_info('monotonic')
namespace(adjustable=False, implementation='clock_gettime(CLOCK_MONOTONIC)', monotonic=True, resolution=1e-09)

As you can see each clock has a different implementation and precision which depends on platform. On MacOS the clock precision for process_time and time clocks is set to 1e-06 which is microseconds. That explains the difference.

You can also use time.clock_getres to get the precision:

In [51]: time.clock_getres(time.CLOCK_REALTIME)
Out[51]: 1.0000000000000002e-06

Further reading:

这篇关于time.time_ns() 在 macOS 上没有正确返回纳秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆