Batch function help?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: Batch function help?

#16 Post by einstein1969 » 04 Jul 2014 10:07

dbenham wrote:I find that advice unworkable. It is hard enough to debug and maintain one version. How can you be sure that your second optimized version is functionally identical to the readable version? Each version would need to be independently tested every time any change is made.

If the project is small this is not true.

dbenham wrote:but that effect is usually minimal compared to other batch bottlenecks.

There are various levels of optimization.
If we talk about optimization push the difference can be large. Have you ever tried to write on the screen using SET?
My experience is still little to argue with you about this. But you know that I did things fast. If you want proof the only thing is to try to rewrite my code in a readable form and you'll see that the speed differency is very large.

dbenham wrote:In every script I have worked on, I have always been able to achieve good, perfectly acceptable performance without resorting to short, unintelligible variable names, remark stripping, or indent/space stripping. Could I squeeze out a bit more performance by using those techniques? - Yes. But I've never found it worthwhile. And I hate reading code that does use those techniques.

Try to do something that requires more speed and you will think again.
On your way to work makes you think things can not get more complex with the dos. Pay attention!

with respect
einstein1969

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

Re: Batch function help?

#17 Post by foxidrive » 04 Jul 2014 10:15

dbenham wrote:How can you be sure that your second optimized version is functionally identical to the readable version? Each version would need to be independently tested every time any change is made.

I strongly agree with Squashman.

I understand that length of code can impact performance, but that effect is usually minimal compared to other batch bottlenecks. (I'm assuming the script is on a reasonably fast storage medium)


+1

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

Re: Batch function help?

#18 Post by einstein1969 » 04 Jul 2014 10:25

foxidrive wrote:
dbenham wrote:How can you be sure that your second optimized version is functionally identical to the readable version? Each version would need to be independently tested every time any change is made.

I strongly agree with Squashman.

I understand that length of code can impact performance, but that effect is usually minimal compared to other batch bottlenecks. (I'm assuming the script is on a reasonably fast storage medium)


+1


-1 :D

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

Re: Batch function help?

#19 Post by Squashman » 04 Jul 2014 10:40

einstein1969 wrote:Try to do something that requires more speed and you will think again.

You don't think Snakey.bat requires a lot of speed optimization?

JWinslow23
Posts: 58
Joined: 17 Jun 2014 10:38

Re: Batch function help?

#20 Post by JWinslow23 » 04 Jul 2014 12:06

I actually think I could stand to make two versions if I have to: one regular and readable, and one golfed. But for right now, I just want it to work. It does in its current state, so thank you. I'll credit all of you in my project (it involves this).

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

Re: Batch function help?

#21 Post by einstein1969 » 05 Jul 2014 12:32

Squashman wrote:
einstein1969 wrote:Try to do something that requires more speed and you will think again.

You don't think Snakey.bat requires a lot of speed optimization?


The code written by dbenham for snake is well made and uses techniques that I still do not know. So I can not know if it can be further optimized. But it seems to me very fast. There are things that are unthinkable, for example, be done in batches. I'm developing a thing (a game?) that is among the 2.5D and pseudo 3D . But still I do not know if it will be possible to make the code readable optimized. I'd like to make two versions: a legible and no. Why is everyone's desire to learn from a readable.


einstein1969

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

Re: Batch function help?

#22 Post by einstein1969 » 05 Jul 2014 12:37

JWinslow23 wrote:I actually think I could stand to make two versions if I have to: one regular and readable, and one golfed. But for right now, I just want it to work. It does in its current state, so thank you. I'll credit all of you in my project (it involves this).


When you do something the first step is to work (there are functional tests). Then seen to be used there is always a second step. The performance tests. They are a must as it is a must that first of all has to work.

einstein1969

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

Re: Batch function help?

#23 Post by foxidrive » 05 Jul 2014 12:44

einstein1969 wrote:But still I do not know if it will be possible to make the code readable optimized. I'd like to make two versions: a legible and no. Why is everyone's desire to learn from a readable.


einstein1969


Can you show us some code where the uncommented version is more than 10% faster than the commented version?
I'm curious to see the difference in the real world and compare them on my computer.

I'm not sure that macros count, as they are a special case.

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

Re: Batch function help?

#24 Post by einstein1969 » 05 Jul 2014 13:48

foxidrive wrote:
einstein1969 wrote:But still I do not know if it will be possible to make the code readable optimized. I'd like to make two versions: a legible and no. Why is everyone's desire to learn from a readable.


einstein1969


Can you show us some code where the uncommented version is more than 10% faster than the commented version?
I'm curious to see the difference in the real world and compare them on my computer.

I'm not sure that macros count, as they are a special case.


The problem is not only the comments. But also comment affect.

I chose a piece of code that matches your "arbitrary" 10%

On my OLD monocore machine (windows seven 32bit) the difference is this:

Code: Select all

21:46:54,22 - 21:46:57,70 elapsed 348 cs
21:46:57,71 - 21:47:02,04 elapsed 433 cs
21:47:02,04 - 21:47:05,98 elapsed 394 cs

348 is 20% more speed than 433
348 is 12% more speed than 394



Code: Select all

@echo off & setlocal EnableDelayedExpansion

rem Reference test

set t0=%time%
For /L %%i in (1,1,100000) do (

  set /a i=i+1

)
call :difftime "%t0%" "%time%" t1

rem ::::::::::::::::::::::::::::::::::::::::

set t0=%time%
For /L %%i in (1,1,100000) do (

  rem this is a comment

  set /a i=i+1

  rem this is a comment

)
call :difftime "%t0%" "%time%" t2

rem ::::::::::::::::::::::::::::::::::::::::

rem the "rem" if faster then rem<SPACE>

set t0=%time%
For /L %%i in (1,1,100000) do (

  rem

  set /a i=i+1

  rem

)
call :difftime "%t0%" "%time%" t3

rem ::::::::::::::::::::::::::::::::::::::::

echo(

rem calculate percent

set /a p1=100-t1*100/t2

echo %t1% is %p1%%% more speed than %t2%

set /a p1=100-t1*100/t3

echo %t1% is %p1%%% more speed than %t3%

exit /b


:difftime
  setlocal
   set t0=%~1
   set t1=%~2
   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
  endlocal & set %3=%a%
exit /b


can you post your result?

einstein1969

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

Re: Batch function help?

#25 Post by Squashman » 05 Jul 2014 16:01

WOW! Way to skew the results in your favor by adding an extra comment in there. One line of code isn't going to have another comment after that. Get real man!

I5 Quad Core with your extra comment.

Code: Select all

C:\Batch>comment_test.bat
16:42:55.60 - 16:42:57.59 elapsed 199 cs
16:42:57.59 - 16:42:59.97 elapsed 238 cs
16:42:59.98 - 16:43:02.16 elapsed 218 cs

199 is 17% more speed than 238
199 is 9% more speed than 218


Without the extra comment.

Code: Select all

C:\Batch>comment_test.bat
16:45:31.20 - 16:45:33.22 elapsed 202 cs
16:45:33.22 - 16:45:35.44 elapsed 222 cs
16:45:35.44 - 16:45:37.56 elapsed 212 cs

202 is 10% more speed than 222
202 is 5% more speed than 212


I could really care less about 2 tenths of a second over 100,000 iterations!!!!
Not too mention 99.9% I would say all my comments are outside my FOR commands.

My initial comments were about readability. Stringing together a bunch of commands with the ampersand makes code extremely unreadable. I can deal with no comments but I can't deal with that kind of unread-ability. Heck I can even deal with single character variables but when people starting using symbols that is just irritating. No offense Jeb, your code is still pretty readable because you do space things out. Granted it does help distinguish between environmental variables and tokens.

Really, your example is relative to what you are doing so this really isn't a good example of performance.

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

Re: Batch function help?

#26 Post by einstein1969 » 05 Jul 2014 16:28

Squashman wrote:WOW! Way to skew the results in your favor by adding an extra comment in there. One line of code isn't going to have another comment after that. Get real man!

I5 Quad Core with your extra comment.

Code: Select all

C:\Batch>comment_test.bat
16:42:55.60 - 16:42:57.59 elapsed 199 cs
16:42:57.59 - 16:42:59.97 elapsed 238 cs
16:42:59.98 - 16:43:02.16 elapsed 218 cs

199 is 17% more speed than 238
199 is 9% more speed than 218


Without the extra comment.

Code: Select all

C:\Batch>comment_test.bat
16:45:31.20 - 16:45:33.22 elapsed 202 cs
16:45:33.22 - 16:45:35.44 elapsed 222 cs
16:45:35.44 - 16:45:37.56 elapsed 212 cs

202 is 10% more speed than 222
202 is 5% more speed than 212


I could really care less about 2 tenths of a second over 100,000 iterations!!!!
Not too mention 99.9% I would say all my comments are outside my FOR commands.

My initial comments were about readability. Stringing together a bunch of commands with the ampersand makes code extremely unreadable. I can deal with no comments but I can't deal with that kind of unread-ability. Heck I can even deal with single character variables but when people starting using symbols that is just irritating. No offense Jeb, your code is still pretty readable because you do space things out. Granted it does help distinguish between environmental variables and tokens.

Really, your example is relative to what you are doing so this really isn't a good example of performance.


You're cheating. You are mixing what I'm saying. I responded to the "quote" "partially" of foxidrive.

But I will say the same. In another case, I would not have responded.

1) You are attentive to the fact that I'm cheating but you do not realize that the code I'm measuring is without FOR. FOR The only serves to better measure.

2) Also I put two comments just because I want to comment WELL on my code. Or your.
The comment after or before the command not change the result!

3) In addition, we are measuring not only the comments. But the variables as you pointed out yourself. So if you do the calculations (this time do not cheat) you see that it goes up!

It 'obvious that depends on the code you're running. We are talking in general with particular examples.

I do not like to be misunderstood.

Code: Select all

 0:29:49,38 -  0:29:52,77 elapsed 339 cs
 0:29:52,77 -  0:29:58,89 elapsed 612 cs
 0:29:58,91 -  0:30:04,82 elapsed 591 cs

339 is 45% more speed than 612
339 is 43% more speed than 591


Code: Select all

@echo off & setlocal EnableDelayedExpansion

rem Reference test

set t0=%time%
For /L %%i in (1,1,100000) do (

  set /a $=$+1

)
call :difftime "%t0%" "%time%" t1

rem ::::::::::::::::::::::::::::::::::::::::

set t0=%time%
For /L %%i in (1,1,100000) do (

  rem this is a comment

  set /a Variable_name=variable_name+1

)
call :difftime "%t0%" "%time%" t2

rem ::::::::::::::::::::::::::::::::::::::::

rem the "rem" if faster then rem<SPACE>

set t0=%time%
For /L %%i in (1,1,100000) do (

  rem

  set /a Variable_name=Variable_name+1

)
call :difftime "%t0%" "%time%" t3

rem ::::::::::::::::::::::::::::::::::::::::


echo(

rem calculate percent

set /a p1=100-t1*100/t2

echo %t1% is %p1%%% more speed than %t2%

set /a p1=100-t1*100/t3

echo %t1% is %p1%%% more speed than %t3%

exit /b


:difftime
  setlocal
   set t0=%~1
   set t1=%~2
   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
  endlocal & set %3=%a%
exit /b



@all
can you post your results?

with respect
einstein1969

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

Re: Batch function help?

#27 Post by Squashman » 05 Jul 2014 18:09

I did post my results in my previous post. One with your two comments and one with one comment.
Like I said before, this is a pointless example because I am pretty sure most people will agree with me that they keep their comments above the FOR command and not inside the FOR command.

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

Re: Batch function help?

#28 Post by ShadowThief » 05 Jul 2014 18:36

einstein1969 wrote:@all
can you post your results?


From my i7-3770K

Code: Select all

20:34:09.58 - 20:34:11.25 elapsed 167 cs
20:34:11.26 - 20:34:14.07 elapsed 281 cs
20:34:14.08 - 20:34:16.79 elapsed 271 cs

167 is 41% more speed than 281
167 is 39% more speed than 271

"More speed." I like it.

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

Re: Batch function help?

#29 Post by Squashman » 05 Jul 2014 20:58

And again you go to the extreme with your example instead of doing something like this.
Here is some interesting output. Showing the difference between variable length. You do get an initial speed boost with a single character variable but after that the time savings is negligible. The initial single variable saves time but after that 2 hundredths of a second over 100K iterations is not going to kill me. Neither would the 4 tenths.

Code: Select all

C:\Batch>varLen_test.bat
single character variable
21:50:11.64 - 21:50:13.10 elapsed 146 cs
two character variable
21:50:13.10 - 21:50:14.97 elapsed 187 cs
three character variable
21:50:14.98 - 21:50:16.87 elapsed 189 cs
four character variable
21:50:16.88 - 21:50:18.80 elapsed 192 cs
five character variable
21:50:18.80 - 21:50:20.74 elapsed 194 cs

146 is 22% more speed than 187
146 is 23% more speed than 189
146 is 24% more speed than 192
146 is 25% more speed than 194


But again, back to my initial point. Mine was not really about comments. It was about his coding style with stacking many commands together with Ampersands.

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

Re: Batch function help?

#30 Post by Squashman » 05 Jul 2014 21:31

And here is my point. Proper coding style with indentation for readability makes absolutely NO SPEED DIFFERENCE.

Code: Select all

@echo off
set t0=%time%
call :scrabbleScore "This test case should return sixty-six points" score
echo Phrase: This test case should return sixty-six points
echo Score: %score%
echo(

set "myString=This phrase is supposed to be worth exactly one hundred twenty points in Scrabble"
call :scrabbleScoreVar myString score
echo Phrase: %myString%
echo Score: %score%
call :difftime "%t0%" "%time%" t1

pause >nul
exit /b

:scrabbleScore phrase score -- Calculates and returns the Scrabble score of a phrase in quotes
::                          -- phrase [in]: phrase to calculate score of (must be in quotes)
::                          -- score [out]: variable name to store result to
if "%~2" neq "" set ?=%1&call :scrabbleScoreVar ? %~2
exit /b

:scrabbleScoreVar phrasevar score -- Calculates and returns the Scrabble score of a variable
::                                -- phrasevar [in]: variable name to calculate score of
::                                -- score [out]   : variable name to store result to
setlocal enabledelayedexpansion
set _=A!%~1!
set #=0
for /l %%g in (12,-1,0) do (
   set /a "#|=1<<%%g"
   for %%h in (!#!) do if "!_:~%%h,1!"=="" set /a "#&=~1<<%%g"
)
set/a #-=1,$=0
set @=!%~1!
for /l %%g in (0,1,%#%) do (
   call set j=%%@:~%%g,1%%
   for %%h in (A a E e I i L l N n O o R r S s T t U u) do if !j!==%%h set /a $+=1
   for %%h in (D d G g) do if !j!==%%h set /a $+=2
   for %%h in (B b C c M m P p) do if !j!==%%h set /a $+=3
   for %%h in (F f H h V v W w Y y) do if !j!==%%h set /a $+=4
   for %%h in (K k) do if !j!==%%h set /a $+=5
   for %%h in (J j X x) do if !j!==%%h set /a $+=8
   for %%h in (Q q Z z) do if !j!==%%h set /a $+=10
)
endlocal&if "%~2" neq "" set %~2=%$%
exit /b

:difftime
  setlocal
   set t0=%~1
   set t1=%~2
   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
  endlocal & set %3=%a%
exit /b

Scrabble0 is the original without indentation and the ampersands. ScrabbleN is what is above.

Code: Select all

C:\Batch>scrabbleO.bat
Phrase: This test case should return sixty-six points
Score: 66

Phrase: This phrase is supposed to be worth exactly one hundred twenty points in
 Scrabble
Score: 120
22:29:20.20 - 22:29:20.38 elapsed 18 cs

C:\Batch>scrabbleN.bat
Phrase: This test case should return sixty-six points
Score: 66

Phrase: This phrase is supposed to be worth exactly one hundred twenty points in
 Scrabble
Score: 120
22:29:28.70 - 22:29:28.87 elapsed 17 cs

Post Reply