python时间函数纳秒_time.time_ns()在MacOS上没有正确返回纳秒?

关键在于精准。python中的每个时钟都有相应的精度,这可以解释您所经历的差异。让我们看看MacBook Pro 2018和MacOS Mojave的时钟细节。python3.7通过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)

请注意

implementation

resolution

. 以下是相同的信息,但来自运行在Ubuntu服务器上的虚拟机:

>>> 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)

正如您所看到的,每个时钟都有不同的实现和精度,这取决于平台。在MacOS上

process_time

time

时钟设置为

1e-06

也就是微秒。这就解释了区别。

您也可以使用

time.clock_getres

要获得精度:

In [51]: time.clock_getres(time.CLOCK_REALTIME)

Out[51]: 1.0000000000000002e-06

进一步阅读:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值