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
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 are faster than constants, as long as you don't have too many variables.
is faster than:
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:
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.
To test your routine for speed improvements, you can use the TI variable like this:
. 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!
- RENUMBER 0,1 - This gains memory because all the references to line numbers in the
program will be shorter.
- Extra space removal - For example IF A=40 THEN 23 can just as well be written
- ; removal. In print statements, the ";" sign can often be removed. For example:
PRINTA$;"-HERE" can just as well be written: PRINTA$"-HERE"
- Using the right kind of variables! If you just use loose variables, then A= will always
use less memory than A%=. Because both uses 7 bytes in memory to store the variable, while A%
uses one extra byte everywhere in the program where you use it. And floating point is
faster anyway, so...
- Single character variable names! (A$ etc) Using two letters (AR$ etc) takes one extra byte for every reference.
- Using the right kind of arrays. Here, integers, such as DIM A%(100) uses less space
than floating point (DIM A(100)). (About half the space in memory).
But be aware that this can fool you. Because if you have many references to the array in the
program, then the integer array can very well use more memory in total. This is most likely
to be the case when you have few variables in the array, for example DIM A%(5) etc...
To find out, just try to replace them and look at the FRE(0) value at run time. The Programmer's Aid's
CHANGE command is useful.
- Putting as much as possible on every program line. Every new line takes some extra bytes.
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.