Batch is slow by design. The main reason is that half of the commands are external utilities. That means as soon as you call such a tool a new process has to be scheduled by the operating system. That takes a lot of time, especially if you call it umpteen times from within a loop. Whenever you have the choice use an internal command rather than an external. I wrote a little batch code that computes the commands outputted by the HELP command and displays whether they are internal functions of cmd.exe or external utilities.
Code: Select all
@echo off &setlocal EnableDelayedExpansion
set /a int=0, ext=0
for /f %%i in ('help^|findstr /rbc:"[A-Z][ABCDEFGHIJKLMNOPQRSTUVWXYZ]"') do (
for /f "tokens=1,2 delims=?" %%j in ("%%i.exe?%%i.com") do (
if "%%~$PATH:j%%~$PATH:k"=="" (
set /a int+=1
set "line=%%i "
echo !line:~,50! - internal
) else (
set /a ext+=1
echo %%i "%%~$PATH:j%%~$PATH:k"
)
)
)
echo(
echo internal %int%
echo external %ext%
pause>nul
We had some discussions in the past how to speed up the start of external tools. E.g. normally you would simply write FINDSTR to call C:\Windows\System32\findstr.exe. That means the process has to search the tool using two variables. 1) the directories saved in %PATH% and 2) the file extension saved in %PATHEXT%. That takes time. Instead you could empty these variables and call the tools by their full name.
Avoid using GOTO LABEL or CALL :LABEL if you can use a FOR (/L) loop instead. Labels have to be searched in the batch file. The order is top to down. It resumes searching at the top of the file if the end was reached without finding it. The longer the file the more time it takes.
But generally spoken: If you have a performance-critical task then don't use batch and prefer compiled languages over interpreted languages.
Steffen