call or not to call was the question.
Sometimes there are tips to use call to reparse a line a second time, usefull for pseudo-array access or inside of loops/brackets.
Code: Select all
for /L %%n in (1,1,10) DO (
set cnt=%%n
echo %cnt% fails
call echo %%cnt%% works
set array[%%n]=Content%%n
call echo %%array[%%n]%%
)
Obviously the same problem can be solved with delayed expansion,
but are there differences?
Yes, many.
-call echo can produce unexpected results
Code: Select all
call echo ;=^/,,^==^ ?hasta? la Vista
Outputs the help screen of the call command
- call can crash the cmd-window
Code: Select all
set "caret=^"
call echo Vista crash %%caret%%
- call is extremely slow
Code: Select all
setlocal EnableDelayedExpansion
set "x=1"
set start=%time%
for /L %%n in (1,1,1000) DO (
rem
CALL set var=!x!
)
set end=%time%
Masured on my system:
without call: 70ms
with one call: 3690ms
with two calls: 7330ms
with three calls: 11270ms
Perhaps it is slow, as it always creates a new variable context and has to copy all variables.
I tested this with a creation of a big bunch of variables (100*5000byte=500kB).
without call: 20ms
with one call: 5420ms
with one call, but yMAX=1: 3720ms
Code: Select all
setlocal EnableDelayedExpansion
set "y="
set yMAX=100
for /L %%n in (1,1,5000) do set "y=!y!#"
for /L %%n in (1,1,%yMAX%) do (
set array%%n=!y!
)
set "x=1"
echo start
set start=%time%
for /L %%n in (1,1,1000) DO (
rem
call rem
)
set end=%time%
echo end
call :bl.DateTime.timeDiff res "%start%" "%end%"
echo %res%
Result: Try to avoid call, better use the delayed expansion
jeb
EDIT: I append the real cause and the problematic side effects of call in another post in this thread
CALL me, or better avoid call#16