脚本如何提高时间精度呢 - V2EX
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 2471 次点击
    这是一个创建于 1093 天前的主题,其中的信息可能已经有所发展或是发生改变。

    游戏(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 啊,确实我试试
    table cellpadding="0" cellspacing="0" border="0" width="100%"> 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     Solana     5796 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 39ms UTC 06:16 PVG 14:16 LAX 23:16 JFK 02:16
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86