nathanielbabiak 2020-12-04 21:50 (Edited)
In this post "small" and "large" refer to the number of tokens required.
Non-zero values (int and float) evaluate to true, these are smaller and faster:
IF X<>0 THEN
with IF X THEN
IF X<>0 AND Y<>0 THEN
with IF X*Y THEN
IF X=0 OR Y=0 THEN
with IF X*Y=0 THEN
There's two non-zero evaluation tricks if you're sure X
and Y
will always have the same sign:
IF X<>0 OR Y<>0 THEN
with IF X+Y THEN
IF X=0 AND Y=0 THEN
with IF X+Y=0 THEN
There's a few more non-zero evaluation tricks when X
(and Y
) are integers:
IF X AND %001=%001 THEN
with IF X AND %001 THEN
IF X AND %010=%010 THEN
with IF X AND %010 THEN
IF X AND %100=%100 THEN
with IF X AND %100 THEN
IF X<=-Y OR X>=Y THEN
with IF X\Y THEN
IF X>-Y AND X<Y THEN
with IF X\Y=0 THEN
This switch-case
structure is small but slow:
IF X=7 THEN
'...
ELSE IF X=6 THEN
'...
ELSE IF X=5 THEN
'...
ELSE IF X=4 THEN
'...
ELSE IF X=3 THEN
'...
ELSE IF X=2 THEN
'...
ELSE IF X THEN
'X=1
'...
ELSE
'X=0
'...
END IF
This switch-case
structure is fast but large:
IF X AND %100 THEN
IF X AND %010 THEN
IF X AND %001 THEN
'X=7
'...
ELSE
'X=6
'...
END IF
ELSE
IF X AND %001 THEN
'X=5
'...
ELSE
'X=4
'...
END IF
END IF
ELSE
IF X AND %010 THEN
IF X AND %001 THEN
'X=3
'...
ELSE
'X=2
'...
END IF
ELSE
IF X AND %001 THEN
'X=1
'...
ELSE
'X=0
'...
END IF
END IF
END IF
The continue
keyword can also be implemented:
FOR X=0 TO 7
CALL FOO
'IF TEST1 THEN "CONTINUE"
IF NOT TEST1 THEN
CALL BAR
CALL ZOT
END IF
'NO CODE ALLOWED HERE, "END IF" AND "NEXT" MUST BE ADJACENT.
NEXT X
To return a value from a subprograms in a simple variable, the variable name must be declared prior. If an algorithm needs more than 3 new variable names, this is smaller than declaring one variable per-line. It's slower only because of overhead, otherwise the same speed O(n).
DECLARE:
DATA 0, 0, 0, 0, 0, 0, 0, 0
LOAD DECLARE
READ Y0, Y1, Y2, Y3, Y4, Y5, Y6, Y7
Single-command if-then statements are smaller (and sometimes faster too) when written on a single line like this:
IF TEST1 THEN CALL FOO
IF TEST1 THEN CALL FOO ELSE CALL BAR
IF TEST1 THEN CALL FOO ELSE IF TEST2 THEN CALL BAR ELSE CALL ZOT
This loop is small but slow:
FOR X=0 TO 7
TEXT 0,X,"-"
NEXT X
This unrolled loop is fast but large:
TEXT 0,0,"-"
TEXT 0,1,"-"
TEXT 0,2,"-"
TEXT 0,3,"-"
TEXT 0,4,"-"
TEXT 0,5,"-"
TEXT 0,6,"-"
TEXT 0,7,"-"
This is a sample loop:
DIM LIST(XHI-1)
FOR X=0 TO XHI-1
CALL FOO(LIST(X),B*X+C)
NEXT X
The sample loop calculates the subtraction twice before entering the loop, then again at each iteration of the loop. This loop only calculates it once, it's faster and larger:
XMAX=XHI-1
DIM LIST(XMAX)
FOR X=0 TO XMAX
CALL FOO(LIST(X),B*X+C)
NEXT X
The sample loop calculates math inside the loop. This loop uses a separate variable to track the math, it's faster and larger:
XMAX=XHI-1
DIM LIST(XMAX)
Y=C
FOR X=0 TO XMAX
CALL FOO(LIST(X),Y)
ADD Y,B
NEXT X
The separate-variable loop uses instructions inside the loop to track the math. This loop uses the for-variable itself, it's faster and larger (and also the code doesn't work, but I hope you get the point):
X=0
XMAX=XHI-1
DIM LIST(XMAX)
YMAX=B*XMAX+C
FOR Y=C TO YMAX STEP B
CALL FOO(LIST(X),Y)
INC X
NEXT Y
Note the step variable B
, if replaced by a calculation, would be calculated at each iteration of the loop, even if B
is negative (e.g. -3
is a calculation of "negation" on the constant 3
).
moechofe 2020-12-05 07:29
good stuff