推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
shinonome
V2EX  ›  Python

脚本如何提高时间精度呢

  •  
  •   shinonome · Oct 13, 2022 · 3149 views
    This topic created in 1443 days ago, the information mentioned may be changed or developed.

    游戏(king of thieves)正在做一个脚本,功能很简单就是点击一下就好了,但是对于点击的时间有很高的要求 time.sleep 我查了下只能做到毫秒级的 i=140 time.time()-start=0.14237737655639648 i=141 time.time()-start=0.14109015464782715 i=142 time.time()-start=0.14684534072875977 i=143 time.time()-start=0.14805078506469727 i=144 time.time()-start=0.14444518089294434 i=145 time.time()-start=0.15004920959472656 有什么其他办法吗,精度的差距有点大

    7 replies  •  2022-10-21 18:07:42 +08:00
    bthulu
        1
    bthulu  
       Oct 13, 2022   ❤️ 1
    不要 sleep, 就 white(true)里死循环查询当前时间就行了
    xtreme1
        2
    xtreme1  
       Oct 13, 2022   ❤️ 1
    假定 Windows 了
    Win 上默认时间片我记得是 15.625ms
    想精确到 1ms 常规操作应该是用多媒体定时器
    shinonome
        3
    shinonome  
    OP
       Oct 13, 2022
    @bthulu 啊,确实我试试
    shinonome
        4
    shinonome  
    OP
       Oct 13, 2022
    @xtreme1 感谢大佬,我研究一下
    ysc3839
        5
    ysc3839  
       Oct 14, 2022   ❤️ 1
    不要 sleep ,用 time.monotonic_ns()或者 time.perf_counter_ns()取时间然后 while 等待。
    https://docs.python.org/3/library/time.html#time.monotonic
    shinonome
        6
    shinonome  
    OP
       Oct 21, 2022
    @ysc3839 感谢,确实好用,精确不少
    ysc3839
        7
    ysc3839  
       Oct 21, 2022
    @ysc3839 翻了下 CPython 的代码,time.monotonic_ns() 在 Windows 下调用的是 GetTickCount64 ,这个精度可能没那么高。
    time.perf_counter_ns() 调用的是 QueryPerformanceCounter ,这个精度是没问题的。
    可以用 time.get_clock_info('perf_counter') 查看精度
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2513 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 07:30 · PVG 15:30 · LAX 00:30 · JFK 03:30
    ♥ Do have faith in what you're doing.