TS1000
For the Experienced Basic Programmer
This column is for the reader who already programs in Basic, and is ready to improve his skills and become a better programmer.
Have you ever written a FOR NEXT loop which ran too darn slow? The first thing you do is make sure you don’t have any statements inside the loop which don’t have to be inside the loop. But what if it’s still too slow? This article concerns a method of speeding up FOR/NEXT loops and GOTO and GOSUB.
First, here is something for you to try: Load a long program, the longer the better. Then, enter the following code; it doesn’t matter if you’re over-writing the program you just loaded from tape.
9900 FOR N=1 TO 50
9910 NEXT N
9920 STOP
Now Put your machine in SLOW mode and run this loop (RUN 9900) and time it– get a clock and count seconds from the moment you hit ENTER after RUN 9900 until the report code 9/9920 appears on the screen.
Now enter the same code at line 10 like this:
10 FOR N=1 TO 50
20 NEXT N
30 STOP
Run that loop and time it.
Surprise! It runs faster at the lower line number. A lot faster. In my test, I got 15 seconds versus 2 seconds.
Well, let’s try another experiment. This time we’ll test GOSUB’s.
9901 RETURN
9910 GOSUB 9901
9920 GOSUB 9901
9930 GOSUB 9901
9940 GOSUB 9901
9950 GOSUB 9901
9960 GOSUB 9901
9970 GOSUB 9901
9980 GOSUB 9901
9990 GOSUB 9901
9995 STOP
Make sure the machine is in slow mode, then RUN 9910 and time it.
I got 6 seconds. Now try this:
1 RETURN
10 GOSUB 1
20 GOSUB 1
30 GOSUB 1
40 GOSUB 1
50 GOSUB 1
60 GOSUB 1
70 GOSUB 1
80 GOSUB 1
90 GOSUB 1
95 STOP
When I RUN I get less than 1 second. So nine GOSUB’s and RETURN’s take 6 seconds at the end of a 16K program and less than 1 second at the beginning. The same thing happens with GOTO.
Why? Apparently, when the Sinclair wants to find a line number, it starts at the beginning of the program and looks at each line number in order until it finds the one it’s looking for.
So if your FOR/NEXT loop at line number 5500 runs too slow, put it near the beginning of the program. Likewise, if a particular section of your code has a lot of GOTO’s you might want to make that section a sub-routine, and put it near the beginning of the program.
Program Line Organization
All of this suggests a different organization of program lines than one might otherwise use. Here is the system I use.
Line Number Contents
0-99 Machine Code Routines
and begin statement
100-999 FOR/NEXT loops
1000-7999 Sub-routines
8000-9499 Main Program
9500-9999 Utilities
The machine code routines are contained in REM statements. The first line after these REM statements is a begin statement (GOTO 8000), so the user can start the program simply by entering RUN or GOTO 1.
I leave so much room for sub-routines because my main program is usually only 20 lines or so. More about that in the next column.
Last of all, the utilities are those routines which help you write the program, but aren’t actually part of the program. An example is the “Bytes Remaining” utility listed elsewhere in this newsletter.
From Friendly Newsletter Vol 1 #1