Received wisdom has it that Commodore didn't want to pay Microsoft extra for updated BASIC interpreters, and thus very little changed in Commodore BASIC v2 from the PETs to the C64. But consider the following program:
5 PRINT
10 PRINT "FOR:"
20 T=TIME
30 FOR I=1 TO 7138
40 NEXT I
50 DT=(TIME-T)/60
60 PRINT I;"LOOPS IN";DT;"S:";INT(I/DT);"/S"
70 PRINT "GOTO:"
80 T=TIME
90 FOR I=1 TO 4588
100 GOTO 110
110 NEXT I
120 DT=(TIME-T)/60
130 PRINT I;"LOOPS IN";DT;"S:";INT(I/DT);"/S"
135 PRINT
Run on an NTSC C64, it outputs:
FOR:
7139 LOOPS IN 10 S: 713 /S
GOTO:
4589 LOOPS IN 10 S: 458 /S
But on a PET (almost any model), the results are:
FOR:
7139 LOOPS IN 9.93333334 S: 718 /S
GOTO:
4589 LOOPS IN 13.3166667 S: 344 /S
So while the FOR
loops run about the same speed on both machines, the PET's GOTO
is roughly ⅓ slower than the C64's.
So my question: what changed in the code to improve this performance? (And perhaps a secondary question: was this improvement made by Microsoft and reflected in other MS 6502 interpreters?)
Further to WimC's comment I renumbered the test as suggested to ensure that the high byte of the GOTO
target was higher than that of the current line:
5 PRINT
10 PRINT "FOR:"
20 T=TIME
30 FOR I=1 TO 7138
40 NEXT I
50 DT=(TIME-T)/60
60 PRINT I;"LOOPS IN";DT;"S:";INT(I/DT);"/S"
70 PRINT "GOTO:"
80 T=TIME
90 FOR I=1 TO 4588
100 GOTO 310
310 NEXT I
320 DT=(TIME-T)/60
330 PRINT I;"LOOPS IN";DT;"S:";INT(I/DT);"/S"
335 PRINT
The C64 results stay the same, but on an emulated PET the GOTO
rate is faster:
FOR:
7139 LOOPS IN 9.93333334 S: 718 /S
GOTO:
4589 LOOPS IN 9.95 S: 461 /S
TI
, but I like my code legible. Anyone who wants BASIC to be efficient is missing the point.TIME
variables exist in Locomotive BASIC (300 Hz) and BBC BASIC (100 Hz). This code is a small subset of code I need to run on as many platforms as possible. If I'd just writtenTI
it wouldn't have run on these other platforms and its purpose wouldn't be clear to those of us who didn't start with C= BASIC. So clean, portable is what this is.