Style and Speed in BASIC

Authors

Publication

Pub Details

Date

Pages

When you write a BASIC program, the order in which you carry out your tasks affects the speed of your program. This routine lets you see simple examples that show how the same task can take 42 seconds at the front of a program , but 78 at the end. When you’ve seen these results, you may rethink your notion to put a screenful of REM’s at the start of your program.

Additional Seconds Required for 5000 GOSUBS at Selected Places

Call fromTopMidBottom
TopX+6+11
Mid+12+18+23
Bottom+25+31+36

Your 2068 and your ZX/TS (in FAST mode) take the same additional time for these tasks. For 2068’s, X=42 seconds. In FAST, ZX/TS’s take only 35 seconds because they use no time to control the screen. In SLOW mode, ZX/TS’s will take 5-6 times longer — both the fastest time and the penalty get magnified.

SYNTAX measured these results using the listings reproduced here. Note that content of program lines is irrelevant — only the number of lines to skip affects the time. Also, these programs are not long, but we traverse them many times.

You can rerun these tests with your changes. We just changed the target of the GOSUB to 10, 700, or 1000 as desired. We always started with a clear screen and began the run with GOTO 2, 200, or 2000.

Next we wondered if FOR-NEXT loops would take variable times, so we deleted lines 3, 300, and 3000, then tested. Sure enough, loops in the middle took 6 extra seconds and loops at the end took 11. Again, the 2068 came out a second slower.

SYNTAX suggests the following rules to make your programs faster when that’s your goal. Remember, speed costs readability.

  • Locate setups, REM’s, and seldom-used pieces at the end.
  • Put often-used items and their calling routines at the front o Minimize the number of pro¬ gram lines preceding loops.
  • Trade complex instructions for fewer lines preceding loops.
  • Avoid subroutines called a lot by a loop near the end of the program — repeat the code in the loop instead.
   2 FOR I=1 TO 5000
3 GOSUB 10
4 NEXT I
5 STOP
10 REM
99 RETURN
100 REM
110 REM
120 REM
130 REM
140 REM
150 REM
160 REM
170 REM
180 REM
190 REM
200 FOR I=1 TO 5000
300 GOSUB 10
400 NEXT I
500 STOP
700 REM
799 RETURN
800 REM
810 REM
820 REM
830 REM
840 REM
850 REM
860 REM
870 REM
880 REM
890 REM
1000 REM
1999 RETURN
2000 FOR I=1 TO 5000
3000 GOSUB 10
4000 NEXT I
5000 STOP

Products

 

Downloadable Media

 

Image Gallery

Source Code

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

People

No people associated with this content.

Scroll to Top