V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2022-10-13 10:23:07 +08:00 · 2054 次点击
    这是一个创建于 553 天前的主题,其中的信息可能已经有所发展或是发生改变。

    游戏(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 条回复    2022-10-21 18:07:42 +08:00
    bthulu
        1
    bthulu  
       2022-10-13 14:02:38 +08:00   ❤️ 1
    不要 sleep, 就 white(true)里死循环查询当前时间就行了
    xtreme1
        2
    xtreme1  
       2022-10-13 14:11:58 +08:00   ❤️ 1
    假定 Windows 了
    Win 上默认时间片我记得是 15.625ms
    想精确到 1ms 常规操作应该是用多媒体定时器
    shinonome
        3
    shinonome  
    OP
       2022-10-13 15:44:19 +08:00
    @bthulu 啊,确实我试试
    shinonome
        4
    shinonome  
    OP
       2022-10-13 15:44:40 +08:00
    @xtreme1 感谢大佬,我研究一下
    ysc3839
        5
    ysc3839  
       2022-10-14 12:18:39 +08:00   ❤️ 1
    不要 sleep ,用 time.monotonic_ns()或者 time.perf_counter_ns()取时间然后 while 等待。
    https://docs.python.org/3/library/time.html#time.monotonic
    shinonome
        6
    shinonome  
    OP
       2022-10-21 16:42:36 +08:00
    @ysc3839 感谢,确实好用,精确不少
    ysc3839
        7
    ysc3839  
       2022-10-21 18:07:42 +08:00
    @ysc3839 翻了下 CPython 的代码,time.monotonic_ns() 在 Windows 下调用的是 GetTickCount64 ,这个精度可能没那么高。
    time.perf_counter_ns() 调用的是 QueryPerformanceCounter ,这个精度是没问题的。
    可以用 time.get_clock_info('perf_counter') 查看精度
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1368 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 23:45 · PVG 07:45 · LAX 16:45 · JFK 19:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.