Batch function help?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: Batch function help?

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

The biggest time savings in the scrabble score code would be to change the formatting of the FOR and IF statements as Foxidrive noted. This saves 3 hundredths of a second. Too me that is a larger time savings then the FOR loop time savings you were showing. 100,000 iterations VS a phrase with a couple dozen letters.

Code: Select all

for /l %%g in (0,1,%#%) do (
   call set j=%%@:~%%g,1%%
   for %%h in (A E I L N O R S T U) do if /I !j!==%%h set /a $+=1
   for %%h in (D G) do if /I !j!==%%h set /a $+=2
   for %%h in (B C M P) do if /I !j!==%%h set /a $+=3
   for %%h in (F H V W Y) do if /I !j!==%%h set /a $+=4
   if /I !j!==K set /a $+=5
   for %%h in (J X) do if /I !j!==%%h set /a $+=8
   for %%h in (Q Z) do if /I !j!==%%h set /a $+=10
)

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

Re: Batch function help?

#32 Post by Squashman » 05 Jul 2014 21:49

But Antonio's code WINS hands down!!!!!

Code: Select all

C:\Batch>scrabbleA.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:47:53.60 - 22:47:53.69 elapsed 9 cs

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

Re: Batch function help?

#33 Post by foxidrive » 05 Jul 2014 21:52

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

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


You misunderstood me.

I want a real example of one of your programming tasks, one script with comments and the same script without comments and NOT a contrived script that is designed to run slowly.

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

Re: Batch function help?

#34 Post by Squashman » 05 Jul 2014 22:12

foxidrive wrote:You misunderstood me.

I want a real example of one of your programming tasks, one script with comments and the same script without comments and NOT a contrived script that is designed to run slowly.

Exactly. I can certainly see the point with the single character environmental variables but the FOR loop with comments was just a poor example that basically had no real value. Nobody puts comments inside their FOR commands. I know I normally don't.

Most the lag time in my scripts are network related and saving a few hundredths of a second within the batch isn't going to save me any time at all. Most of my slow downs are how much network traffic is going on or how much crap is running on the server. I do automation for a living and each of my servers runs close to 200+ automated tasks and each task may run multiple times a day.

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

Re: Batch function help?

#35 Post by foxidrive » 05 Jul 2014 23:09

Squashman wrote:
foxidrive wrote:You misunderstood me.

I want a real example of one of your programming tasks, one script with comments and the same script without comments and NOT a contrived script that is designed to run slowly.

Exactly. I can certainly see the point with the single character environmental variables but the FOR loop with comments was just a poor example that basically had no real value. Nobody puts comments inside their FOR commands. I know I normally don't.

Most the lag time in my scripts are network related and saving a few hundredths of a second within the batch isn't going to save me any time at all. Most of my slow downs are how much network traffic is going on or how much crap is running on the server. I do automation for a living and each of my servers runs close to 200+ automated tasks and each task may run multiple times a day.


Network traffic is a big variable for you but your point is the one that einstein should focus on - how many seconds does a script without comments save for a real world task.

Dave's point is also an important one in real life - if einstein has two versions of every script then, to be certain that no error has crept in, he has to test both scripts every time a change is made.

It's einstein's choice if he maintains two sets of scripts but the three of us, and probably very many others, don't see any value in doing it.

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Batch function help?

#36 Post by pieh-ejdsch » 06 Jul 2014 04:52

To reduce the if's you can use every Loop once a time.
A negative for loop is Much efficent as two if's.
Minus 30%

Code: Select all

for /l %%g in (0,1,%#%) do (
 call set j=%%@:~%%g,1%%
 (for /f "delims=AaEeIiLlNnOoRrSsTtUu" %%h in ("!j!") do @
 )||set /a $+=1
 (for /f "delims=DdGg" %%h in ("!j!") do @
 )||set /a $+=2
 (for /f "delims=BbCcMmPp" %%h in ("!j!") do @
 )||set /a $+=3
 (for /f "delims=FfHhVvWwYy" %%h in ("!j!") do @
 )||set /a $+=4
 if /i K == !j!  set /a $+=5
 (for /f "delims=JjXx" %%h in ("!j!") do @
 )||set /a $+=8
 (for /f "delims=QqZz" %%h in ("!j!") do @
 )||set /a $+=10
)


Phil

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

Re: Batch function help?

#37 Post by einstein1969 » 06 Jul 2014 07:53

Squashman wrote:But Antonio's code WINS hands down!!!!!

Code: Select all

C:\Batch>scrabbleA.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:47:53.60 - 22:47:53.69 elapsed 9 cs



Code: Select all

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
15:55:42,49 - 15:55:42,54 elapsed 5 cs


Code: Select all

@echo off
setlocal EnableDelayedExpansion

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

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" equ "" exit /B
set "phrase=A%~1"
goto s

: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
if "%~2" equ "" exit /B
set "phrase=A!%~1!"

:s
(
set Len=0
for /l %%g in (12,-1,0) do (
  set /a "Len|=1<<%%g"
  for %%h in (!Len!) do if "!phrase:~%%h,1!"=="" set /a "Len&=~1<<%%g"
)
for /l %%g in (1,1,!Len!) do (
 for /f "delims=" %%a in ("!phrase:~%%g,1!") do (
  (for /f "delims=DdGg" %%h in ("%%a") do @
  )||set /a Len+=1
  (for /f "delims=BbCcMmPp" %%h in ("%%a") do @
  )||set /a Len+=2
  (for /f "delims=FfHhVvWwYy" %%h in ("%%a") do @
  )||set /a Len+=3
  if /i K == %%a set /a Len+=4
  (for /f "delims=JjXx" %%h in ("%%a") do @
  )||set /a Len+=7
  (for /f "delims=QqZz" %%h in ("%%a") do @
  )||set /a Len+=9
  (for /f "delims=- " %%h in ("%%a") do @
  )|| set /a Len-=1
 )
)
if "%~2" neq "" set %~2=!Len!
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


Continue to make blunders. The timer dos batch is not accurate. There are 3 cs error or more I prefer to test longer (with a FOR) to avoid this type of error.

einstein1969

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

Re: Batch function help?

#38 Post by einstein1969 » 06 Jul 2014 10:47

foxidrive wrote:
einstein1969 wrote:The problem is not only the comments. But also comment affect.

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


You misunderstood me.

I want a real example of one of your programming tasks, one script with comments and the same script without comments and NOT a contrived script that is designed to run slowly.


One example is the code of MD5. There are varius version. The last version use few comment for advanced speed.

Another example that use variable name short or with $ is the crc calculator.

But if you search in the forum thare are other that used a code non well readable.

I don't view code from you...

Why you don't ask in the forum? Why you don't open thread? or why you don't develop any thing?

You get the code of other and zip only?

Get a code fast that use a lot o cpu. Then change variable name, variable lenght, add comment and post here your test.

Do something too? or merely ask?

Edit: I have seen few post from you... But for simple things. Show more!

einstein1969
Last edited by einstein1969 on 06 Jul 2014 11:15, edited 1 time in total.

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

Re: Batch function help?

#39 Post by einstein1969 » 06 Jul 2014 11:01

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


Now only indentation is your issue?

you and I do not know in depth the Dos batch.

Than there are cases that indentation can decrease performance?

I think that there are cases... If you want know open a thread and ASK to community!

einstein1969

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

Re: Batch function help?

#40 Post by einstein1969 » 06 Jul 2014 11:05

pieh-ejdsch wrote:To reduce the if's you can use every Loop once a time.
A negative for loop is Much efficent as two if's.
Minus 30%

Code: Select all

for /l %%g in (0,1,%#%) do (
 call set j=%%@:~%%g,1%%
 (for /f "delims=AaEeIiLlNnOoRrSsTtUu" %%h in ("!j!") do @
 )||set /a $+=1
 (for /f "delims=DdGg" %%h in ("!j!") do @
 )||set /a $+=2
 (for /f "delims=BbCcMmPp" %%h in ("!j!") do @
 )||set /a $+=3
 (for /f "delims=FfHhVvWwYy" %%h in ("!j!") do @
 )||set /a $+=4
 if /i K == !j!  set /a $+=5
 (for /f "delims=JjXx" %%h in ("!j!") do @
 )||set /a $+=8
 (for /f "delims=QqZz" %%h in ("!j!") do @
 )||set /a $+=10
)


Phil


This is very clever 8)

thanks so much!

einstein1969

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Batch function help?

#41 Post by pieh-ejdsch » 06 Jul 2014 14:44

Finally you can reduce the changin loops (7 doubled processes) into:
one call and two for loops with positive results.

See on this:
http://www.administrator.de/forum/batch-input-so-abgreifen-als-w%c3%a4re-die-tastatur-auf-en-us-gestellt-233504.html#comment-913924

Phil

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

Re: Batch function help?

#42 Post by einstein1969 » 06 Jul 2014 22:46

pieh-ejdsch wrote:Finally you can reduce the changin loops (7 doubled processes) into:
one call and two for loops with positive results.

See on this:
http://www.administrator.de/forum/batch-input-so-abgreifen-als-w%c3%a4re-die-tastatur-auf-en-us-gestellt-233504.html#comment-913924

Phil


I have read but I could not understand ...

Can you explain?

einstein1969

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

Re: Batch function help?

#43 Post by foxidrive » 07 Jul 2014 04:25

einstein1969 wrote:
foxidrive wrote:
einstein1969 wrote:The problem is not only the comments. But also comment affect.

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


You misunderstood me.

I want a real example of one of your programming tasks, one script with comments and the same script without comments and NOT a contrived script that is designed to run slowly.


One example is the code of MD5. There are varius version. The last version use few comment for advanced speed.

Another example that use variable name short or with $ is the crc calculator.

But if you search in the forum thare are other that used a code non well readable.

I don't view code from you...

Why you don't ask in the forum? Why you don't open thread? or why you don't develop any thing?

You get the code of other and zip only?

Get a code fast that use a lot o cpu. Then change variable name, variable lenght, add comment and post here your test.

Do something too? or merely ask?

Edit: I have seen few post from you... But for simple things. Show more!

einstein1969



Nice rant.

It's obvious that you can't back up your claim - case closed.

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

Re: Batch function help?

#44 Post by Squashman » 07 Jul 2014 06:43

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


Now only indentation is your issue?

you and I do not know in depth the Dos batch.

Than there are cases that indentation can decrease performance?

I think that there are cases... If you want know open a thread and ASK to community!

einstein1969

I specifically said the coding style he was using and referred several times to, comments, ampersands and single character variables.
I specifically rewrote the code to be more readable by removing the ampersands and put each SET statement on its own line and there was absolutely no SPEED increase with the original code versus my code. In fact, several times I ran it, my code was a hundredth of a second faster. But again that could be a gazillion different things on the system that could affect that speed difference.

You are the one that wanted to prove that the original coding style was faster. When I changed it, it wasn't. Heck I even did more testing then you did to prove that your single character variable did improve performance.

But as we have all realized the bottleneck to most code is the algorithm you use to solve it.

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

Re: Batch function help?

#45 Post by einstein1969 » 07 Jul 2014 08:49

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


Now only indentation is your issue?

you and I do not know in depth the Dos batch.

Than there are cases that indentation can decrease performance?

I think that there are cases... If you want know open a thread and ASK to community!

einstein1969

I specifically said the coding style he was using and referred several times to, comments, ampersands and single character variables.
I specifically rewrote the code to be more readable by removing the ampersands and put each SET statement on its own line and there was absolutely no SPEED increase with the original code versus my code. In fact, several times I ran it, my code was a hundredth of a second faster. But again that could be a gazillion different things on the system that could affect that speed difference.

You are the one that wanted to prove that the original coding style was faster. When I changed it, it wasn't. Heck I even did more testing then you did to prove that your single character variable did improve performance.

But as we have all realized the bottleneck to most code is the algorithm you use to solve it.


You, as foxidrive have misunderstood ...

Keep things I put in my mouth I did not say.

I did not say that all the code is affected by this.

I have already answered this thing.

Here's what you missed ...

einstein1969 wrote: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.


EDIT:In addition, to rewrite the algorithm, because is not optimal, it is part of software development.

einstein1969

Post Reply