BASIC OPTIMATION

The following is true for all versions of the Commodore 8 bit Basic (I think).

GOTO and GOSUB

Because of the format of the basic, where every basic line only have one pointer to the next line and none to the previous, then jumps (GOTO and GOSUB) will take different time to execute depending on the situation. Jumps to lines just below the current one will be very fast, while jumps to lines above the current line will be very fast for the first lines of the program but slower and slower the lower down in the program the line is. So if you have a big program and on the last line you jump one line up to the second last line, then this will be quite slow as the basic will have to go through every line pointer in the program. The RETURN command is however always of the same speed.

The tip here is to always put your heavy main routine FIRST in your program, only preceded by one line that just jumps down to the parts setting up your program etc. Then to go back to the main routine at the second first line. For example:

  0 GOSUB 500 : REM SETUP EVERYTHING
  1 ...Main routine
Heavily used subroutines should also be put as high as possible or just below the calling routine.

When your program is ready, you can also renumber it with RENUMBER 0,1 (if you have a vic-20, you will need something like Programmer's aid for this). This way the line numbers will be as small as possible, saving space and also speeding it up a little.

VARIABLES

Variables are faster than constants, as long as you don't have too many variables. For example:

A=132:FORT=1TO100:B=PEEK(A):NEXT

is faster than:

FORT=1TO100:B=PEEK(132):NEXT

Another important issue is that different variables has different speed! The thing to remember here is that the variables used first in the program is the ones that will be the fastest. So a good way to speed your program up is to analyze your heavy routine to see which variables that are used the most. Then just use them (set some value to them) the first thing you do in your program. For example, on line number 0:

0 X=1:L=1:T=1:R=1

After that, X will be fastest, followed by L, and then T, and then R...

One could be fooled to think that integer variables (A% etc) would be faster than floating point variables (A etc), but it's the other way around! Normal floating point variables are the fastest.

TESTING SPEED

To test your routine for speed improvements, you can use the TI variable like this:
  10 E=TI
  .
  .
  . your routine
  .
  .
  120 PRINT TI-E
The faster it gets, the lower is the value displayed. But note that this won't be reliable if you are using disk access in your routine.

OUT OF MEMORY?

If you want a memory efficiant basic program, then here are a few tips!

COMPILERS

If you compile your program into machine language (for example using the Austro compiler), then none of these rules are true. In fact, constants are then faster than variables and integers are then faster than floating point. So it would be wise do decide if you are going to compile or not before you start to design your program.

Good luck!

Anders Persson
https://www.boray.se


Back