You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

time.monotonic_ns与time.sleep短时间交互异常问题问询

time.monotonic_ns() 与 time.sleep() 在Windows下出现0纳秒差值的异常分析

问题描述

在Windows 11设备上运行Python 3.9.13(3.12版本也存在类似问题,仅IDLE终止行为不同)时,发现time.monotonic_ns()与time.sleep()存在异常交互:

import time

def main():
    while True:
        start = time.monotonic_ns()
        time.sleep(0.01)  # 10毫秒
        end = time.monotonic_ns()
        diff = end - start
        print("diff: {}".format(diff))

if __name__ == "__main__":
    main()
  • 多数情况下输出约15000000纳秒(15-16毫秒),符合系统最小睡眠时间(约8毫秒)加上调度延迟的预期;
  • 偶尔会输出0纳秒,即使缩短sleep时长,结果仍在15毫秒或0纳秒之间波动;
  • 改用time.time_ns()时每次输出均非零(结果随机,最低约11毫秒),time.perf_counter_ns()则完全正常。

原因分析

  1. Windows时钟实现差异:
    • Python的time.monotonic_ns()在Windows上基于系统单调时钟实现,而time.perf_counter_ns()使用更高精度的性能计数器(依赖QueryPerformanceCounter),后者专为短时间间隔测量优化,稳定性更好;
    • 尽管官方文档标注time.sleep()的分辨率为100纳秒,但Windows系统默认定时器分辨率为15.625毫秒,sleep实际会受系统调度和时钟分辨率影响,monotonic_ns()的采样在某些线程唤醒场景下可能出现异常对齐,导致两次读取值完全一致。
  2. Python版本特定实现问题:
    • 在Python 3.9到3.12的Windows版本中,monotonic_ns()的底层实现可能存在线程唤醒时的时钟采样bug,导致偶尔出现时钟值未更新的情况。

解决方案

若需稳定测量sleep前后的时间间隔,推荐使用time.perf_counter_ns()替代time.monotonic_ns(),它不仅保持单调递增的特性,还提供了更高的精度和稳定性,完全适配短时间间隔测量场景:

import time

def main():
    while True:
        start = time.perf_counter_ns()
        time.sleep(0.01)
        end = time.perf_counter_ns()
        diff = end - start
        print("diff: {}".format(diff))

if __name__ == "__main__":
    main()

内容的提问来源于stack exchange,提问作者Twistios_Player

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 01:23:30