Improving Batch File for loop with start subcommand

前端 未结 3 793
小蘑菇
小蘑菇 2020-12-21 23:40

I\'ve currently got a very simple script which pings 10 IPs:

@ECHO OFF
for /L %%i in (100, 1, 110) DO (
  START /W /B cmd /c ping -n 1 192.168.0.%%i | find \         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-22 00:12

    You have no need of start command. You turn off all it's features. Then you tell it to start a new process for CMD which is slow and unnecessary. So - ping -n 1 192.168.0.%%i |find "time=".

    See my summary here of how to start programs - What do all commands in batch mean and do?.

    You can use start to run ping in parallel, but you need to remove /w which means wait (but it's being ignored because) and /b which says don't run the command in a new spawned process but do it sequentially.

    As you are still doing multiple process creation of Ping you can use WMIC to avoid that and test all computers in one process create.

    wmic /node:@"c:\computerlist.txt" /append:"textfile.txt" path win32_pingstatus where "address='127.0.0.1' and responsetime > 100" get responsetime,timestamprecord.

    So we've gone from 20 process creates to 1.

    In programming we have to pay taxes of system overhead. Windows collect most tax at Process Creation and Window Creation. This is to allow other operations to take place without as much system overheads. An example is Windows keeps tables of processes that are running - uses resources to update when a process is created but allows quick lookup for other operations.

提交回复
热议问题