Speed Issue with blank variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dondon
Posts: 1
Joined: 17 Jun 2016 09:03

Speed Issue with blank variables

#1 Post by dondon » 17 Jun 2016 10:20

I have some speed issues regarding this code.I want to use a blank varriable but this requires conveting symbol to value and value to symbol each and every time.The speed issue is about 1.5 s but i must reduce it to 1 s.Without the conversion it would take about 0.01 seconds and i can do a timer.But empty values must be read as blank.Any idea how could i avoid it and keep the speed up

NOTE:In this poste is a 3x3 matrix, i use 10x10 matrix and thats why i have the speed issue

Code: Select all

@echo off

@rem Symbols
set full=X
set empty=O

@rem Cells
set cell_ini=0
set cell_max=9

:Cell
set /a cell_ini=%cell_ini%+1
set c%cell_ini%=%Empty%
if %cell_ini% NEQ %cell_max% goto Cell

:Display

:Convert_Empty_to_Blank
set s1=0
if %c1% EQU %empty% set s1=1
if %c1% EQU %empty% set c1=

set s2=0
if %c2% EQU %empty% set s2=1
if %c2% EQU %empty% set c2=

set s3=0
if %c3% EQU %empty% set s3=1
if %c3% EQU %empty% set c3=

set s4=0
if %c4% EQU %empty% set s4=1
if %c4% EQU %empty% set c4=

set s5=0
if %c5% EQU %empty% set s5=1
if %c5% EQU %empty% set c5=

set s6=0
if %c6% EQU %empty% set s6=1
if %c6% EQU %empty% set c6=

set s7=0
if %c7% EQU %empty% set s7=1
if %c7% EQU %empty% set c7=

set s8=0
if %c8% EQU %empty% set s8=1
if %c8% EQU %empty% set c8=

set s9=0
if %c9% EQU %empty% set s9=1
if %c9% EQU %empty% set c9=

cls
echo.
echo  I---I
echo  I%c1%%c2%%c3%I
echo  I%c4%%c5%%c6%I
echo  I%c7%%c8%%c9%I
echo  I---I
echo.


:Convert_Blank_to_Empty
if %s1% EQU 1 set c1=%empty%
if %s2% EQU 1 set c2=%empty%
if %s3% EQU 1 set c3=%empty%
if %s4% EQU 1 set c4=%empty%
if %s5% EQU 1 set c5=%empty%
if %s6% EQU 1 set c6=%empty%
if %s7% EQU 1 set c7=%empty%
if %s8% EQU 1 set c8=%empty%
if %s9% EQU 1 set c9=%empty%


:Toggle_Values
set /a Toggle=%random%/ (32767 / 9)+1
goto C%Toggle%


:C1
set s=0

if %c1% EQU %Empty% set s=0
if %c1% EQu %full% set s=1

if %s% EQU 0 set c1=%full%
if %s% EQU 1 set c1=%Empty%

goto Display

:C2
set s=0

if %c2% EQU %Empty% set s=0
if %c2% EQu %full% set s=1

if %s% EQU 0 set c2=%full%
if %s% EQU 1 set c2=%Empty%

goto Display

:C3
set s=0

if %c3% EQU %Empty% set s=0
if %c3% EQu %full% set s=1

if %s% EQU 0 set c3=%full%
if %s% EQU 1 set c3=%Empty%

goto Display

:C4
set s=0

if %c4% EQU %Empty% set s=0
if %c4% EQu %full% set s=1

if %s% EQU 0 set c4=%full%
if %s% EQU 1 set c4=%Empty%

goto Display

:C5
set s=0

if %c5% EQU %Empty% set s=0
if %c5% EQu %full% set s=1

if %s% EQU 0 set c5=%full%
if %s% EQU 1 set c5=%Empty%

goto Display

:C6
set s=0

if %c6% EQU %Empty% set s=0
if %c6% EQu %full% set s=1

if %s% EQU 0 set c6=%full%
if %s% EQU 1 set c6=%Empty%

goto Display

:C7
set s=0

if %c7% EQU %Empty% set s=0
if %c7% EQu %full% set s=1

if %s% EQU 0 set c7=%full%
if %s% EQU 1 set c7=%Empty%

goto Display

:C8
set s=0

if %c8% EQU %Empty% set s=0
if %c8% EQu %full% set s=1

if %s% EQU 0 set c8=%full%
if %s% EQU 1 set c8=%Empty%

goto Display

:C9
set s=0

if %c9% EQU %Empty% set s=0
if %c9% EQu %full% set s=1

if %s% EQU 0 set c9=%full%
if %s% EQU 1 set c9=%Empty%


goto Display

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Speed Issue with blank varriables

#2 Post by sambul35 » 17 Jun 2016 11:43

This line looks like an error:

Code: Select all

set c%cell_ini%=%Empty%

Without changing the entire logic of your code (possibly faulty or ineffective): given "empty=O" string, try using "s=y" instead of "s=1", FOR /L instead of IF, combine vars in SET in "Convert" blocks, use delayed expansion !var! instead of multiple GOTO in your Toggle code:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

:: some code here...

:Display

:Convert_Empty_to_Blank
for /l %%i in (1,1,9) do (
  if c%%i==%empty% (set "s%%i=y" & set "c%%i=") else (set "s%%i=n"))

:: more code here...
     
:Toggle_Values
set /a "Toggle=%random%/(32767/9)+1"
if !c%Toggle%!==%empty% (set "c%Toggle%=%full%"
) else if !c%Toggle%!==%full% (set "c%Toggle%=%empty%")
goto :Display

:End     
exit /b
Last edited by sambul35 on 18 Jun 2016 05:08, edited 1 time in total.

penpen
Expert
Posts: 1996
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Speed Issue with blank varriables

#3 Post by penpen » 17 Jun 2016 19:42

dondon wrote:I have some speed issues regarding this code.I want to use a blank varriable but this requires conveting symbol to value and value to symbol each and every time.
If you don't need to work with the coverted values, then you only need one direction computing and also you don't need to save the converted values (and using the toggle of sambul35) (untested):

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
@rem Symbols
set "full=X"
set "empty=O"

@rem Cells
set "cell_max=9"
for /l %%a in (1, 1, %cell_max%) do set "c%%~a=%Empty%"


:Display
cls
echo(
echo( I---I
echo( I!c1:%empty%= !!c2:%empty%= !!c3:%empty%= !I
echo( I!c4:%empty%= !!c5:%empty%= !!c6:%empty%= !I
echo( I!c7:%empty%= !!c8:%empty%= !!c9:%empty%= !I
echo( I---I
echo(

:Toggle_Values
set /a "Toggle=(%random% %% cell_max)+1"
if "!C%Toggle%!" == "%empty%" ( set "C%Toggle%=%full%"
) else set "C%Toggle%=%empty%"

goto :Display

penpen

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

Re: Speed Issue with blank varriables

#4 Post by Aacini » 17 Jun 2016 19:53

I am afraid I don't understand what is your question, or even if is there a question here...

If "this requires conveting symbol to value and value to symbol each and every time" is the cause of the speed issues, then the obvious answer is: "don't do those conversions". However, in order to complete this solution it is necessary to modify the code, and here it is where the problems begins...

Your code is particularly confusing to read. For example, you use "Empty" to mean "zero" and "Blank" to mean "space", so reading your code requires an additional effort in order to remember the not-obvious meaning of each variable. In a similar way, when you use "s#" variables to store the previous values of "c#" variables, you store a 1 in "s#" when "c#" is %empty" (that is, zero), and you store a zero when "c#" is not zero! Why don't just use the same values?

You should started this question with an explanation of the code and then describe the problem; otherwise, you are forcing us to review the code in order to understand it and, after that, trying to create a solution... What would you think if someone gives you a solution with not a single explanation at all?

Code: Select all

@echo off
setlocal EnableDelayedExpansion

@rem Symbols
set "Symbols= X"

@rem Cells
set cell_max=9
for /L %%i in (1,1,%cell_max%) do set "c%%i=0"

:Display

(
cls
echo/
echo  I---I
echo  I!Symbols:~%c1%,1!!Symbols:~%c2%,1!!Symbols:~%c3%,1!I
echo  I!Symbols:~%c4%,1!!Symbols:~%c5%,1!!Symbols:~%c6%,1!I
echo  I!Symbols:~%c7%,1!!Symbols:~%c8%,1!!Symbols:~%c9%,1!I
echo  I---I
echo/
)

:Toggle_Values
set /a "Toggle=(%random%*cell_max) / 32768 + 1"
set /a "c%Toggle%^=1"

goto Display

Antonio

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Speed Issue with blank varriables

#5 Post by sambul35 » 18 Jun 2016 05:33

IMHO, why not let the OP to do the leg work and learn (understand & explain) something useful in the process by adapting partial code samples? :D

I now have a few question looking at your posts:

a) in penpen's post, what using %% does in

Code: Select all

set /a "Toggle=(%random% %% cell_max)+1"

b) in Aacini's post, why you concluded the OP uses "Empty" as zero, when he sets "Empty=O" (letter "O")? And what "^" does in

Code: Select all

set /a "c%Toggle%^=1"

May you guys agree the above tricks deserve explanation, otherwise it looks similar to the OP's post, hard to understand and therefore learn from examples. 8)
Last edited by sambul35 on 18 Jun 2016 08:10, edited 2 times in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Speed Issue with blank varriables

#6 Post by aGerman » 18 Jun 2016 07:39

I didn't had deeper insights into the example codes. Just answering your operator questions ...

Both %% and ^ are used in a SET /A statement as operators. See SET /?

%% used in a batch code will be always reduced to a single %. Here it is the modulus operator. The result of a modulus operation is the remainder of a division.

^ is the "Exclusive Bitwise Or" operator (XOR). Combined with the assignment operator ...
set /a "x^=y"
... it is the same as ...
set /a "x=x^y"

Regards
aGerman

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Speed Issue with blank varriables

#7 Post by sambul35 » 18 Jun 2016 07:56

Thanks for clarification. Now its easier to figure out what the entire code does (Bitwise operations). I should look at SET Help as well. :)
Last edited by sambul35 on 18 Jun 2016 12:41, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Speed Issue with blank varriables

#8 Post by aGerman » 18 Jun 2016 09:56

[OT]
sambul35 wrote:I should look at SET Help as well. :)

Actually always a good idea. But in that case I fear it doesn't help much because you'll find only a list of operators not even with their names. Since the operators used for SET /A seem to be derived from C you should rather look for a index of C operators. Perhaps also of interest: Operator Precedence (of course only for operators that are present for SET /P which are only a subset of the C operators ...).
[/OT]

Post Reply