Performance & Optimization of Batch Files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SirJosh3917
Posts: 36
Joined: 02 May 2016 18:59

Performance & Optimization of Batch Files

#1 Post by SirJosh3917 » 29 May 2020 17:18

Hi y'all. I wanted to know, is there a centralized list of knowledge to optimize performance in batch files? Readability and maintainability is not a concern - I'll be using these tips and trips to auto-generate some fast batch code at some point in the future. Currently, I've assembled this list of knowledge (and I'll be updating the OP to include new information shared):

- Use parenthesis to have CMD read your code into memory before it executes (needs source)
WARNING: this trick induces early expansion of % variables. Use SETLOCAL ENABLEDELAYEDEXPANSION and !s to prevent this.

Code: Select all

(
	echo Hello, World!
	set /p "name=Hello> "
	echo Hello, !name!
)
- Perform multiple SET operations at once (needs source)

Code: Select all

set /a "first=0, second=1"
- Perform manual loop unrolling (needs source)

Code: Select all

for /L %%A in (1,1,10) do echo %%A

::turns into

echo 1
echo 2
echo 3
echo 4
echo 5
echo 6
echo 7
echo 8
echo 9
echo 10
- Name variables closer to the start of the alphabet (A) if they are static (not changed often), else name them closer to the end of the alphabet (ZZZZ) if they are dynamic (updated frequently) source

Code: Select all

set A="static"
set Z="dynamic"
echo %Z%
set Z="variable"
echo %Z%
- Try to avoid GOTOs, CALLs, FOR, and other forms of control flow indirection. Instead of GOTOs or CALLs, use macros source

Code: Select all

::Example linked at source for macros
Anyways, if you have any more performance tips & tricks, please do share!

Lowsun
Posts: 29
Joined: 14 Apr 2019 17:22

Re: Performance & Optimization of Batch Files

#2 Post by Lowsun » 29 May 2020 19:47

Wouldn't the first one perform the same, or even slower? In delayed expansion I don't think CMD "reads your code into memory" but expands all the !! variables each code block line, instead of expanding them before the code block.

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: Performance & Optimization of Batch Files

#3 Post by misol101 » 02 Jun 2020 08:34

Ok, here is my no 1 trick: use another language :P (best bet: jscript+minimal batch startup)

It would actually be interesting to know if there is any other common scripting language as slow as batch?


But ok, trying to be more helpful regarding the topic, from what I can remember off the top of my head:

1. Clean out all environment variables at the start of the script (minus the ones you actually need)

Code: Select all

for /f "tokens=1 delims==" %%v in ('set') do if /i not "%%v"=="KEEPTHIS" set "%%v="
(again, it would be interesting to know if there is any other language punishing its users speed-wise for using too many variables?)

2. For the same reason, avoid putting lots of data into environment variables while running. This code:

Code: Select all

if %option%=1 set s="somereallylongstringwithstuffthatIneed1"
if %option%=2 set s="somereallylongstringwithstuffthatIneed2"
if %option%=3 set s="somereallylongstringwithstuffthatIneed3"
is preferable to:

Code: Select all

::early prep code
set s1="somereallylongstringwithstuffthatIneed1"
set s2="somereallylongstringwithstuffthatIneed2"
set s3="somereallylongstringwithstuffthatIneed3"

::later on
for %%a in (!option!) do set s=!s%%a!
(goodbye, nice setup!)

3. For infinite (but breakable) loops, using for /l+goto is much faster than just using goto (or use Aacini's more fancy while loops for similar speed as below: viewtopic.php?t=3487)

Code: Select all

:REPEAT
for /l %%a in (1,1,1000) do if not defined STOP {
  rem set STOP to break
}
if not defined STOP goto REPEAT
(gee, I wonder if there is another language that doesn't have while loops or equivalent?)

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Performance & Optimization of Batch Files

#4 Post by ShadowThief » 02 Jun 2020 18:51

misol101 wrote:
02 Jun 2020 08:34
It would actually be interesting to know if there is any other common scripting language as slow as batch?
Powershell always seems pretty slow to start up

Post Reply