Further analysis of basic.
Here we take a look at GOSUB vs PROC and if LOCAL vars are faster.
GOTO and GOSUB were always dirty words when I was a kid. Many books said to avoid them, some there are many times that you simply cant. With BBC Basic you have PROC, which is much more elegant. The question here is simple “Is it just elegant or are there performance benefits”
GOSUB vs PROC
Lets take a look and see with some actual timing tests.
Here is a simple program that allows us to measure it:
10 MODE 7
20 VDU 23,1,0;0;0;0;
30 PRINT "BBC BASIC PERFORMANCE TESTS"
40 PRINT "==========================="
50 REM GET MACHINE TYPE
60 Q%=2000 : REM DEFAULT TO BBC
70 IF TOP> 300000000 THEN Q%=1000000: PRINT "PC MODE" ELSE Q%=2000: PRINT "BBC MODE"
80 PRINT "GOSUB vs PROC"
90 TGOSUB=0
100 TPROC=0
110 PRINT "TESTING GOSUB"
120 A%=0
130 TIME=0
140 T% = TIME
150 FOR P%=1 TO Q%
160 GOSUB 330
170 NEXT
180 TGOSUB = TIME-T%
190 PRINT "TESTING PROC"
200 A%=0
210 TIME=0
220 T% = TIME
230 FOR P%=1 TO Q%
240 PROC_INCA
250 NEXT
260 TPROC = TIME-T%
270 PRINT "GOSUB TIME:";TGOSUB
280 PRINT "PROC TIME:";TPROC
290 END
300 DEF PROC_INCA
310 A%=A%+1
320 ENDPROC
330 A%=A%+1
340 RETURN
The results show a clear but small advantage:
the PROC is doing less heaving lifting compared with GOSUB.
Nice…
Global vs LOCAL vars in PROC
When declaring LOCAL in a PROC it can be a really nice way to make modular code that can swap in and out of programs without causing problems. Especially when using generic variables such as i and j that tend to appear elsewhere, without messing them up.
So, useful sometimes. But is there a advantage of LOCAL over the easy of coding?
Here is a simple program to draw the comparison. Testing against the A% integer variable, either global of LOCAL.
10 MODE 7
20 VDU 23,1,0;0;0;0;
30 PRINT "BBC BASIC PERFORMANCE TESTS"
40 PRINT "==========================="
50 REM GET MACHINE TYPE
60 Q%=2000 : REM DEFAULT TO BBC
70 IF TOP> 300000000 THEN Q%=1000000: PRINT "PC MODE" ELSE Q%=2000: PRINT "BBC MODE"
80 PRINT "PROC LOCAL vs GLOBAL VARS"
90 TGLOBAL=0
100 TLOCAL=0
110 PRINT "TESTING PROC WITH GLOBAL VAR"
120 TIME=0
130 T% = TIME
140 FOR P%=1 TO Q%
150 PROC_INCGLOBAL
160 NEXT
170 TGLOBAL = TIME-T%
180 PRINT "TESTING PROC WITH LOCAL VAR"
190 TIME=0
200 T% = TIME
210 FOR P%=1 TO Q%
220 PROC_INCLOCAL
230 NEXT
240 TLOCAL = TIME-T%
250 PRINT "GLOBAL TIME:";TGLOBAL
260 PRINT "LOCAL TIME:";TLOCAL
270 END
280 DEF PROC_INCGLOBAL
290 A%=0
300 A%=A%+1
310 ENDPROC
320 DEF PROC_INCLOCAL
330 LOCAL A%
340 A%=A%+1
350 ENDPROC
For the global test we need to set the A% to zero so that is it doing the same thing.
The results show that the local var is actually slower. It is interesting.
when doing this in the local Windows program it is actually faster but on the BBC emulator it is certainly slower.
Lesson learned. If you NEED it to be local then fine. But consider using a global as it may be faster.
Check it out on YouTube: