“echo” in .bat is quite slow

2019-08-11 06:22发布

I wrote a little .bat file, hoping to be able to create a matrix-like (0 and 1) output.

The problem is, that it is pretty slow, it takes nearly two seconds to fill one line.

Is there something I can do to get it run faster?

Matrix.bat:

@echo off
color 02

:start
if %random% LSS 16384 (
echo|set /p=1
) else (
echo|set /p=0
)
goto start

1条回答
干净又极端
2楼-- · 2019-08-11 06:55

echo is very slow. So build your line without echoing the single chars, then echo the whole line at once. Another trick: set /a "l=!random! &1" uses the last Bit from !random! only (so it gives either 0or 1). This is quicker than processing Integer.

@echo off
setlocal ENABLEDELAYEDEXPANSION
:start
set line=
for /L %%i in (1,1,80) do (
    set /a "l=!random! &1"
    set line=!line!!l!
  ) 
  echo !line! 
)
goto start
查看更多
登录 后发表回答