Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
May, 1982. 1982, Issue l$Sue 7& 2.4 COMPUTE! COMPUTlI 143<br />
Considering<br />
Considering<br />
SpaceAnd<br />
Time In The<br />
Atari<br />
Atari<br />
C Michael Levy and G rant Levy<br />
C. Michael Levy and Grant Levy<br />
Gainesville, FL<br />
Would you like to know how to speed up some of<br />
your programs, or make them use up less memory?<br />
As everyone comes to learn as they gam gain experience<br />
in m programming, programmmg, small and often very subtle<br />
changes cha.nges in even very simple programs can n have<br />
major consequences. Consider the following owing four<br />
programs. Each has exactly y 1000 lines. Each makes<br />
two references to variable B, 994 assignments to<br />
variable vanable A, contain six GOSUBs COSUBs and six RETURNs,<br />
one POKE, one PEEK, one PRINT and one STOP.<br />
They all achieve the same end: they determine that<br />
the value of variable A is zero and print the number<br />
of clock ticks (each lasting l/60th 1I60th sec.) required for<br />
execulIon of the program. They are equivalent,<br />
execution of the program. They are equivalent,<br />
right? n glll' Wrong! W1"Dllg!<br />
Programs 1I and 2 each require about 15000<br />
bytes of memory, nearly 66% more than the ap<br />
bytes of memory, nearl y 66% more than the approximately<br />
9100 bytes required for Programs 3<br />
and 4. The reason for this is the way Atari BASIC<br />
handles constants and variables. Each and every<br />
reference to a constant (zero, in these programs)<br />
consumes seven bytes. This is true even if the statements<br />
using those constants are never executed in<br />
the program. (as are lines 2-994 in Program 1I and<br />
hnes lines 8-1000 I in In Program 2). Programs 3 and 4<br />
involve v?lve the ~ssi.g assignment e nt of a constant to only one<br />
vanable variable (B 111 in llIle line I); 1); variable A assumes its values<br />
by reference to this value. Each such reference<br />
requires onlyy one byte.<br />
. So the first lesson is to use constants sparingly,<br />
If at all. Develop a schema for assigning variable<br />
if at all. Develop a schema for assigning variable<br />
names to constants so that youu will not confuse<br />
them with "real" variables. For example, consider<br />
them with "real" variables. For example, consider<br />
DO = 0, 0 I = I , or CO = 0, C I = I , etc. where the<br />
DO = 0, D1 = 1, or CO = 0, C1 = 1, etc. where the<br />
symbol "D" is a mneumonic for digit or "C" reminds<br />
symbol "0 " is a mneumonic for digit o r "C" reminds<br />
you that it is a consta:nt. constant. Any other combination of<br />
letters andlor and/or digits could then represent variables<br />
which V{I1),. vary.<br />
Speed DIfferences<br />
Differences<br />
While Programs I 1 and 2 are identical in memory<br />
requirements, and Programs 3 and 4 are also iden<br />
requirements, and Programs 3 and 4 are also iden<br />
tical to each other in terms of memory, these pairs<br />
of programs are vastly different in execution execUlion speed.<br />
Here we find that Programs 2 and 4 each require<br />
only y one clock tick for completion. In marked<br />
contrast are Programs 1 and 3 which are 42 times<br />
contrast are Programs I and 3 which are 42 times<br />
slower!<br />
slower!<br />
The reason for this enormous discrepancy is<br />
the way that Atari Atan BASIC seems to locate subroutines.<br />
BASIC is apparently incapable of immediately<br />
jumping Jumping to the desired line referenced in a GOSUB. COSUB.<br />
Rather, it appears to start from the first line of or the<br />
program, and sequentially search through the list<br />
pl:o,gram , and sequentially sea rch through the list<br />
of lines hnes until unlIllt it finds what it wants. Thus, in Programs<br />
3 and 4, when it encounters in line 1000 a<br />
GOSUB COS U B 999, it must begin at line one and look at<br />
each of the 998 intervening lines until it reaches<br />
999. There, it encounters a GOSUB COSUB 998. Back to<br />
the top of the list it goes, fruitlessly examining 997<br />
lines. And so on. BASIC performs the same sequence<br />
of steps in Programs 2 and 4, but it obviously<br />
has to do fewer of them, since all of the subroutines<br />
are near the top of the list of line e numbers.<br />
Thus the second lesson, is to place subroutines<br />
as close as possible to the beginning of or each program.<br />
Some of your programs that heretofore<br />
seemed to drag on unmercifully could now have<br />
more zip.<br />
And, finally, we answer the question n that has<br />
been bothering you for some time. No, we are not<br />
masochists. We did no/ not type in 1000 lines of code in<br />
order to perform these benchmark tests. Instead, , a<br />
one-line program was written to create a 1000-line<br />
skeleton for Programs 1I and 2:<br />
10 OPEN #2,8,0,"D:PROGRAM : FORJ= FOR] = 1 TO 1000<br />
:?? #2J;" " A=O": A = 0" : NEXT]: J : CLOSE #2<br />
. Then PROCRAM was ENTERed, the approp<br />
Then PROGRAM was ENTERed, the approp<br />
riate n minor mlllor changes made to only seven lines, and<br />
then RUN. The same procedure was followed for<br />
then RUN. The same procedure was fo llowed for<br />
Program 1.<br />
1 POKE 20,0 : B=O B = 0 ,GOSUB : 1000, : B=PEEK(20)<br />
B = :?B:STOP: ? B : 2 A=O A = 0<br />
3 A=O A = 0<br />
4 A=O A=0<br />
5 A A=O = 0<br />
6 A=O A = 0<br />
7 A=O A = 0<br />
8 A=O A = 0<br />
9 A=O<br />
9 A = 0<br />
ere. etc.<br />
995 A=O: A = 0:RETURN<br />
996 GOSUB 995 : RETURN<br />
997 GOSUB 996 : RETURN<br />
998 GOSUB 997 : RETURN<br />
999 GOSUB 998 : RETURN<br />
GOSUB 995 : RETURN<br />
GOSUB 996 : RETURN<br />
GOSUB 997 : RETURN<br />
999 GOSUB 998 : RETURN<br />
1000 GOSUB 999: RETURN