Jeff-20 wrote:
Just to clarify, is this closer to the top of memory or closer to the beginning of the programs execution?
Is it equally fast when a program directs early to a subroutine that defines all variables?
Code: Select all
0 GOSUB 500
1 REM Insert a 400 line program
...
500 X=1: G=1: T=1: RETURN
It means closer to the beginning of the programs execuction. Yes, your example will work quite as well.
If you have a tight loop that is run, say more than 90% of time, you can do as follows:
Example:
Mandelbrot Set
Here were have this loop:
Code: Select all
17 FORI=1TON:X2=X*X:Y2=Y*Y:IFX2+Y2<4THENXY=X*Y:X=X2-Y2+R:Y=XY+XY+J:NEXT
First of all: It isn't necessary to put I and N into the list of 'early' variables, where both control the FOR loop. CBM BASIC keeps a pointer to I (i.e. fast access all the way) and reads the value of N only once, and keeps that value as well.
Second: Count occurrences. X and Y are accessed 4 times in a loop (read and write); X2, Y2, and XY are accessed 3 times each. R and J only once, but they do have variable contents. Therefore:
Third: I kept 4 as literal value. I could put this in a variable as well, but that would slower access to all other variables. And single digit values aren't quite that bad. Furthermore all other variables are so seldom used (compared to line 17), there is no gain to put them in line 10.
The original program runs for
156 minutes, if I omit line 10 it needs
195 minutes! Both values were quick-checked with VICE (no speed limit, at 5000% speed
), and rounded to the nearest minute. Thus line 10 gives a speed-up by
20%!
In a third run I re-entered line 10, added ':F=4' to line 10, and replaced X2+Y2<4 in line 17 with X2+Y2<F. Result: Also a (slight) slow-down, compared to the original - as I expected above!
Greetings,
Michael