FOR /F and var TEMP , TMP, USERPROFILE

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

FOR /F and var TEMP , TMP, USERPROFILE

#1 Post by einstein1969 » 15 Jul 2014 17:22

Hi,

I have notated that the for command in the form of "execute command" depend from these variables.

If all these variables are not defined the execution time increase (doubles or more)

This is the code for probe:

Code: Select all

@echo off & setlocal EnableDelayedExpansion

set "t0=!time!"
For /L %%N in (1,1,500) do (
   for /F "Tokens=1 delims=" %%v in ('set') do rem
)
set "t1=!time!"
call :difftime

echo(

set "TEMP="
set "USERPROFILE="
set "TMP="

set "t0=!time!"
For /L %%N in (1,1,500) do (
   for /F "Tokens=1 delims=" %%v in ('set') do rem
)
set "t1=!time!"
call :difftime

exit /b

:difftime
  for /F "tokens=1-8 delims=:.," %%a in ("!t0: =0!:!t1: =0!") do set /a "a=(((1%%e-1%%a)*60)+1%%f-1%%b)*6000+1%%g%%h-1%%c%%d, a+=(a>>31) & 8640000"
  echo %t0% - %t1% Elapsed %a% cs
exit /b


I have tested only on windows 7 32bit:

Code: Select all

 1:14:48,77 -  1:14:53,12 Elapsed 435 cs

 1:14:53,13 -  1:15:03,79 Elapsed 1066 cs



can someone confirm?

EDIT:little modify in code for better understanding.
einstein1969
Last edited by einstein1969 on 15 Jul 2014 18:47, edited 2 times in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: FOR /F and var TEMP , TMP, USERPROFILE

#2 Post by Squashman » 15 Jul 2014 17:55

I am not understanding your example or what you are trying to test.

einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: FOR /F and var TEMP , TMP, USERPROFILE

#3 Post by einstein1969 » 15 Jul 2014 18:42

Squashman wrote:I am not understanding your example or what you are trying to test.


The example run 500 times a FOR /F loop and timing this.

Then after deleting from environment all 3 variables, redo the timing.

The results show that the second execution is slower.

I don't know why...

you can probe and post your result for confirm?

EDIT: I have modify the code in previus post

einstein1969

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FOR /F and var TEMP , TMP, USERPROFILE

#4 Post by foxidrive » 15 Jul 2014 19:42

It only occurs when clearing the %temp% variable



with all variables
11:39:20.48 - 11:39:21.84 Elapsed 136 cs
11:39:21.84 - 11:39:23.19 Elapsed 135 cs
11:39:23.19 - 11:39:24.55 Elapsed 136 cs
11:39:24.55 - 11:39:25.91 Elapsed 136 cs
11:39:25.92 - 11:39:27.29 Elapsed 137 cs
removing userprofile
11:39:27.29 - 11:39:28.66 Elapsed 137 cs
11:39:28.66 - 11:39:30.02 Elapsed 136 cs
11:39:30.02 - 11:39:31.37 Elapsed 135 cs
11:39:31.38 - 11:39:32.74 Elapsed 136 cs
11:39:32.74 - 11:39:34.10 Elapsed 136 cs
removing tmp
11:39:34.10 - 11:39:35.46 Elapsed 136 cs
11:39:35.46 - 11:39:36.81 Elapsed 135 cs
11:39:36.81 - 11:39:38.16 Elapsed 135 cs
11:39:38.16 - 11:39:39.51 Elapsed 135 cs
11:39:39.51 - 11:39:40.87 Elapsed 136 cs
removing temp
11:39:40.88 - 11:39:42.80 Elapsed 192 cs
11:39:42.80 - 11:39:44.71 Elapsed 191 cs
11:39:44.72 - 11:39:46.62 Elapsed 190 cs
11:39:46.63 - 11:39:48.53 Elapsed 190 cs
11:39:48.54 - 11:39:50.45 Elapsed 191 cs

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: FOR /F and var TEMP , TMP, USERPROFILE

#5 Post by Aacini » 15 Jul 2014 20:12

FOR /F ... in ('command') must execute the command and store all its output in a temporary file, and then process this file to populate the %%P arameter in each cyle, so I can only guess that the file is stored in the directory given by %temp% variable and, if it is not found, it must find another place to create the temporary file, process that should take more time. I could bet that this problem don't happen if just a string is processed, instead of execute a command...

Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FOR /F and var TEMP , TMP, USERPROFILE

#6 Post by foxidrive » 15 Jul 2014 20:23

This replaces the 'set' command with a number of echo statements.
There is no difference in execution time now.

with all variables
12:21:18.51 - 12:21:19.90 Elapsed 139 cs
12:21:19.90 - 12:21:21.29 Elapsed 139 cs
12:21:21.29 - 12:21:22.68 Elapsed 139 cs
12:21:22.68 - 12:21:24.07 Elapsed 139 cs
12:21:24.08 - 12:21:25.52 Elapsed 144 cs
removing temp
12:21:25.53 - 12:21:26.92 Elapsed 139 cs
12:21:26.92 - 12:21:28.32 Elapsed 140 cs
12:21:28.32 - 12:21:29.72 Elapsed 140 cs
12:21:29.72 - 12:21:31.12 Elapsed 140 cs
12:21:31.12 - 12:21:32.54 Elapsed 142 cs
Press any key to continue . . .


Code: Select all

@echo off 

echo with all variables
for /L %%a in (1,1,5) do call b1
goto :a
echo removing userprofile
set "USERPROFILE="
for /L %%a in (1,1,5) do call b1
echo removing tmp
set "TMP="
for /L %%a in (1,1,5) do call b1
:a
echo removing temp
set "TEMP="
for /L %%a in (1,1,5) do call b1

pause




b1.bat

Code: Select all

@echo off & setlocal EnableDelayedExpansion

set "t0=!time!"
For /L %%N in (1,1,500) do (
   for /F "delims=" %%v in ('
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   ') do rem
)
set "t1=!time!"
call :difftime

exit /b

:difftime
  for /F "tokens=1-8 delims=:.," %%a in ("!t0: =0!:!t1: =0!") do set /a "a=(((1%%e-1%%a)*60)+1%%f-1%%b)*6000+1%%g%%h-1%%c%%d, a+=(a>>31) & 8640000"
  echo %t0% - %t1% Elapsed %a% cs
exit /b

einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: FOR /F and var TEMP , TMP, USERPROFILE

#7 Post by einstein1969 » 16 Jul 2014 06:15

foxidrive wrote:It only occurs when clearing the %temp% variable



with all variables
11:39:20.48 - 11:39:21.84 Elapsed 136 cs
11:39:21.84 - 11:39:23.19 Elapsed 135 cs
11:39:23.19 - 11:39:24.55 Elapsed 136 cs
11:39:24.55 - 11:39:25.91 Elapsed 136 cs
11:39:25.92 - 11:39:27.29 Elapsed 137 cs
removing userprofile
11:39:27.29 - 11:39:28.66 Elapsed 137 cs
11:39:28.66 - 11:39:30.02 Elapsed 136 cs
11:39:30.02 - 11:39:31.37 Elapsed 135 cs
11:39:31.38 - 11:39:32.74 Elapsed 136 cs
11:39:32.74 - 11:39:34.10 Elapsed 136 cs
removing tmp
11:39:34.10 - 11:39:35.46 Elapsed 136 cs
11:39:35.46 - 11:39:36.81 Elapsed 135 cs
11:39:36.81 - 11:39:38.16 Elapsed 135 cs
11:39:38.16 - 11:39:39.51 Elapsed 135 cs
11:39:39.51 - 11:39:40.87 Elapsed 136 cs
removing temp
11:39:40.88 - 11:39:42.80 Elapsed 192 cs
11:39:42.80 - 11:39:44.71 Elapsed 191 cs
11:39:44.72 - 11:39:46.62 Elapsed 190 cs
11:39:46.63 - 11:39:48.53 Elapsed 190 cs
11:39:48.54 - 11:39:50.45 Elapsed 191 cs


I seem that occour in more complicated mode

Try this:

Code: Select all

@echo off 

setlocal

echo with all variables
for /L %%a in (1,1,5) do call b1

rem goto :a

echo removing temp
set "TEMP="
for /L %%a in (1,1,5) do call b1
echo removing tmp
set "TMP="
for /L %%a in (1,1,5) do call b1
:a
echo removing userprofile
set "USERPROFILE="
for /L %%a in (1,1,5) do call b1

pause


result:

Code: Select all

with all variables
12:42:24,48 - 12:42:29,01 Elapsed 453 cs
12:42:29,01 - 12:42:33,54 Elapsed 453 cs
12:42:33,56 - 12:42:38,03 Elapsed 447 cs
12:42:38,03 - 12:42:43,07 Elapsed 504 cs
12:42:43,09 - 12:42:48,00 Elapsed 491 cs
removing temp
12:42:48,00 - 12:42:55,26 Elapsed 726 cs
12:42:55,28 - 12:43:01,32 Elapsed 604 cs
12:43:01,32 - 12:43:07,39 Elapsed 607 cs
12:43:07,40 - 12:43:12,34 Elapsed 494 cs
12:43:12,35 - 12:43:18,81 Elapsed 646 cs
removing tmp
12:43:18,81 - 12:43:23,73 Elapsed 492 cs
12:43:23,75 - 12:43:28,51 Elapsed 476 cs
12:43:28,53 - 12:43:33,10 Elapsed 457 cs
12:43:33,10 - 12:43:37,68 Elapsed 458 cs
12:43:37,68 - 12:43:42,40 Elapsed 472 cs
removing userprofile
12:43:42,42 - 12:43:53,39 Elapsed 1097 cs
12:43:53,39 - 12:44:04,31 Elapsed 1092 cs
12:44:04,31 - 12:44:15,23 Elapsed 1092 cs
12:44:15,23 - 12:44:26,15 Elapsed 1092 cs
12:44:26,15 - 12:44:37,06 Elapsed 1091 cs


b1 is unchanged but your machine is faster. You may need to lift the loop from 500 to 1000 or more

einstein1969

einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: FOR /F and var TEMP , TMP, USERPROFILE

#8 Post by einstein1969 » 16 Jul 2014 06:46

foxidrive wrote:This replaces the 'set' command with a number of echo statements.
There is no difference in execution time now.

with all variables
12:21:18.51 - 12:21:19.90 Elapsed 139 cs
12:21:19.90 - 12:21:21.29 Elapsed 139 cs
12:21:21.29 - 12:21:22.68 Elapsed 139 cs
12:21:22.68 - 12:21:24.07 Elapsed 139 cs
12:21:24.08 - 12:21:25.52 Elapsed 144 cs
removing temp
12:21:25.53 - 12:21:26.92 Elapsed 139 cs
12:21:26.92 - 12:21:28.32 Elapsed 140 cs
12:21:28.32 - 12:21:29.72 Elapsed 140 cs
12:21:29.72 - 12:21:31.12 Elapsed 140 cs
12:21:31.12 - 12:21:32.54 Elapsed 142 cs
Press any key to continue . . .


Code: Select all

@echo off 

echo with all variables
for /L %%a in (1,1,5) do call b1
goto :a
echo removing userprofile
set "USERPROFILE="
for /L %%a in (1,1,5) do call b1
echo removing tmp
set "TMP="
for /L %%a in (1,1,5) do call b1
:a
echo removing temp
set "TEMP="
for /L %%a in (1,1,5) do call b1

pause




b1.bat

Code: Select all

@echo off & setlocal EnableDelayedExpansion

set "t0=!time!"
For /L %%N in (1,1,500) do (
   for /F "delims=" %%v in ('
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   echo a^&
   ') do rem
)
set "t1=!time!"
call :difftime

exit /b

:difftime
  for /F "tokens=1-8 delims=:.," %%a in ("!t0: =0!:!t1: =0!") do set /a "a=(((1%%e-1%%a)*60)+1%%f-1%%b)*6000+1%%g%%h-1%%c%%d, a+=(a>>31) & 8640000"
  echo %t0% - %t1% Elapsed %a% cs
exit /b


I have tested and seem that affect "echo" . I thinks every command...

re-try this:

Code: Select all

@echo off 

setlocal

echo with all variables
for /L %%a in (1,1,5) do call b1

rem goto :a

echo removing temp
set "TEMP="
for /L %%a in (1,1,5) do call b1
echo removing tmp
set "TMP="
for /L %%a in (1,1,5) do call b1
:a
echo removing userprofile
set "USERPROFILE="
for /L %%a in (1,1,5) do call b1

endlocal



result:

Code: Select all

with all variables
14:37:41,47 - 14:37:45,86 Elapsed 439 cs
14:37:45,88 - 14:37:50,32 Elapsed 444 cs
14:37:50,32 - 14:37:54,77 Elapsed 445 cs
14:37:54,79 - 14:37:59,19 Elapsed 440 cs
14:37:59,19 - 14:38:03,64 Elapsed 445 cs
removing temp
14:38:03,66 - 14:38:08,10 Elapsed 444 cs
14:38:08,10 - 14:38:12,77 Elapsed 467 cs
14:38:12,77 - 14:38:17,72 Elapsed 495 cs
14:38:17,72 - 14:38:22,14 Elapsed 442 cs
14:38:22,14 - 14:38:26,55 Elapsed 441 cs
removing tmp
14:38:26,57 - 14:38:30,96 Elapsed 439 cs
14:38:30,97 - 14:38:35,35 Elapsed 438 cs
14:38:35,36 - 14:38:39,80 Elapsed 444 cs
14:38:39,82 - 14:38:44,30 Elapsed 448 cs
14:38:44,32 - 14:38:48,80 Elapsed 448 cs
removing userprofile
14:38:48,82 - 14:38:59,47 Elapsed 1065 cs
14:38:59,49 - 14:39:10,13 Elapsed 1064 cs
14:39:10,14 - 14:39:20,71 Elapsed 1057 cs
14:39:20,72 - 14:39:31,36 Elapsed 1064 cs
14:39:31,38 - 14:39:41,97 Elapsed 1059 cs



einstein1969

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: FOR /F and var TEMP , TMP, USERPROFILE

#9 Post by Squashman » 16 Jul 2014 06:49

einstein1969 wrote:EDIT: I have modify the code in previus post

einstein1969

Yes that makes more sense. You had your SET statements in the wrong spot to test your hypothesis.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FOR /F and var TEMP , TMP, USERPROFILE

#10 Post by foxidrive » 16 Jul 2014 07:25

When using the echo a commands in b1.bat and increasing the loop to 5000 I got these results:

with all variables
23:05:37.13 - 23:05:51.50 Elapsed 1437 cs
23:05:51.50 - 23:06:05.84 Elapsed 1434 cs
23:06:05.84 - 23:06:20.29 Elapsed 1445 cs
removing userprofile
23:06:20.29 - 23:06:34.67 Elapsed 1438 cs
23:06:34.67 - 23:06:49.40 Elapsed 1473 cs
23:06:49.40 - 23:07:03.96 Elapsed 1456 cs
removing tmp
23:07:03.96 - 23:07:18.38 Elapsed 1442 cs
23:07:18.38 - 23:07:32.67 Elapsed 1429 cs
23:07:32.67 - 23:07:47.28 Elapsed 1461 cs
removing temp
23:07:47.28 - 23:08:06.94 Elapsed 1966 cs
23:08:06.94 - 23:08:26.58 Elapsed 1964 cs
23:08:26.58 - 23:08:46.24 Elapsed 1966 cs
Press any key to continue . . .


and a second run with a different order:

with all variables
23:09:33.87 - 23:09:47.94 Elapsed 1407 cs
23:09:47.94 - 23:10:02.03 Elapsed 1409 cs
23:10:02.03 - 23:10:16.22 Elapsed 1419 cs
removing temp
23:10:16.23 - 23:10:30.71 Elapsed 1448 cs
23:10:30.71 - 23:10:44.80 Elapsed 1409 cs
23:10:44.80 - 23:10:59.03 Elapsed 1423 cs
removing userprofile
23:10:59.04 - 23:11:13.21 Elapsed 1417 cs
23:11:13.22 - 23:11:27.40 Elapsed 1418 cs
23:11:27.41 - 23:11:41.55 Elapsed 1414 cs
Press any key to continue . . .

Post Reply