How to optimize booleans...

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
User avatar
XavSnap
Posts: 1940
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

How to optimize booleans...

Post by XavSnap »

Hi all,

If you had to use booleans conditions, in a game, the speed can be decreased by all of conditions.

Exemple:

Code: Select all

5 LET Z=0
10 FOR X=0 TO 31
20 FOR Y=0 TO 21
30 IF (X=1 OR X=5 OR X=10 OR X=20 OR X=30 OR X=31) AND Z=1 THEN PRINT AT Y,X;"\::";
40 IF (X=1 OR X=3 OR X=7 OR X=12 OR X=18 OR X=25 OR X=31) AND Z=0 THEN PRINT AT Y,X;"\::";
45 IF NOT (X=1 OR X=5 OR X=10 OR X=20 OR X=30 OR X=31) AND NOT (X=1 OR X=3 OR X=7 OR X=12 OR X=18 OR X=25 OR X=32) THEN PRINT AT Y,X;".";
50 LET Z=NOT Z
60 NEXT Y
70 NEXT X
You had to check two values : X and Z.
To speed up the process, you had to create a double (multidimensional) boolean array : TEST( Z , X ) where Z=1 or 0 and X= 0>31 (columns), but in fact,1&2 and 1>32 to avoid then zero in the array TEST(Z+1 , X+1)
All conditions will be precalculated in the array for Z=0 (+1) and Z=1(+1).
Just call the array to check the right value...

Note: 0=FALSE and 1 (or value<>0)=TRUE
A numeric array take room in the memory, about 7 bytes per value : 7*2*32= 448 b !!!

Code: Select all

1 DIM T(2,32)
2 FOR X=0 TO 32
3 IF (X=1 OR X=5 OR X=10 OR X=20 OR X=30 OR X=31) THEN LET T(1,X+1)=1
4 IF (X=1 OR X=3 OR X=7 OR X=12 OR X=18 OR X=25 OR X=31) THEN LET T(2,X+1)=1
5 NEXT X
9 LET Z=0
10 FOR X=0 TO 31
20 FOR Y=0 TO 21
30 IF T(Z+1,X+1) THEN PRINT AT Y,X;"\::"
40 IF NOT T(Z+1,X+1) THEN PRINT AT Y,X;"."
50 LET Z=NOT Z
60 NEXT Y
70 NEXT X
Note:
30 IF T(Z+1,X+1) THEN PRINT AT Y,X;"\::"
40 IF NOT T(Z+1,X+1) THEN PRINT AT Y,X;"."
equal:
30 PRINT AT Y,X;"."
40 IF T(Z+1,X+1) THEN PRINT AT Y,X;"\::"
or
30 PRINT AT Y,X;".";AT Y,X;("\::" AND T(Z+1,X+1))
;)


Have fun…

P files to test the speed up:
P1.P
(1.36 KiB) Downloaded 147 times
P2.P
(1.67 KiB) Downloaded 146 times
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
Post Reply