BBC Basic Optimise 2

Further tips for improving BBC Basic.

In this article we look at the SPC() command and VDU 31

SPC() Command

The SPC command simply returns a sequence of space characters. It can be used as a replacement of string literals containing a sequence of space characters.

   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%=1000 : REM DEFAULT TO BBC
   70 IF TOP> 300000000 THEN Q%=200000: PRINT "PC MODE" ELSE Q%=1000: PRINT "BBC MODE"
   80 PRINT "SPC vs STRING TIMER V1"
   90 TSTRINGSHORT=0
  100 TSTRINGLONG=0
  110 TSPCSHORT=0
  120 TSPCLONG=0
  130 TSTRINGBLANK=0
  140 TSPCBLANK=0
  150 TIME=0
  160 T% = TIME
  170 FOR P%=1 TO Q%
  180   PRINT TAB(0,4);"TEST     TEST"
  190 NEXT P%
  200 TSTRINGSHORT = TIME-T%
  210 TIME=0
  220 T% = TIME
  230 FOR P%=1 TO Q%
  240   PRINT TAB(0,5);"TEST                    TEST"
  250 NEXT P%
  260 TSTRINGLONG = TIME-T%
  270 TIME=0
  280 T% = TIME
  290 FOR P%=1 TO Q%
  300   PRINT TAB(0,6);"TEST";SPC(6);"TEST"
  310 NEXT P%
  320 TSPCSHORT = TIME-T%
  330 TIME=0
  340 T% = TIME
  350 FOR P%=1 TO Q%
  360   PRINT TAB(0,7);"TEST";SPC(20);"TEST"
  370   PRINT
  380 NEXT P%
  390 TSPCLONG = TIME-T%
  400 TIME=0
  410 T% = TIME
  420 FOR P%=1 TO Q%
  430   PRINT TAB(0,8);"                    "
  440 NEXT P%
  450 TSTRINGBLANK = TIME-T%
  460 TIME=0
  470 T% = TIME
  480 FOR P%=1 TO Q%
  490   PRINT TAB(0,9);SPC(20)
  500 NEXT P%
  510 TSPCBLANK = TIME-T%
  520 PRINT TAB(0,10)
  530 PRINT "STRING TIME SHORT:";TSTRINGSHORT
  540 PRINT "STRING TIME LONG:";TSTRINGLONG
  550 PRINT "SPC TIME SHORT:";TSPCSHORT
  560 PRINT "SPC TIME LONG:";TSPCLONG
  570 PRINT "STRING TIME BLANK:";TSTRINGBLANK
  580 PRINT "SPC TIME BLANK:";TSPCBLANK

The above code performs a timing analysis of the following:

Printing out a string with a small number and large number of repeated spaced.

Printing out a string that contains a small number of spaces using the SPC() command and a long string.

Printing out a simple list of spaces from a string literal with no other text.

Printing out the same list of spaces (20 in this case) using the SPC() command

The results show a mixed bag. Really the only advantage here is when displaying a list of spaces at a given location. The SPC command here, shown at the bottom, is a little faster. It could be used to blank out an area of screen without messing around with string literals. It would also allow that width to change easily with an input integer value!

VDU 31

The VDU 31 command can be used to place the text cursor at a specific location. The next program allows this to be tested to see which is better, VDU 31,x,y or the more typical PRINT TAB(x,y)

   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%=100000: PRINT "PC MODE" ELSE Q%=2000: PRINT "BBC MODE"
   80 PRINT "PRINT vs VDU TIMER V1"
   90 TPRINT=0
  100 TVDU=0
  110 TIME=0
  120 T% = TIME
  130 FOR P%=1 TO Q%
  140   PRINT TAB(0,4);"ABC"
  150 NEXT P%
  160 TPRINT = TIME-T%
  170 TIME=0
  180 T% = TIME
  190 FOR P%=1 TO Q%
  200   VDU 31,0,5,65,66,67
  220 NEXT P%
  230 TVDU = TIME-T%
  240 PRINT TAB(0,10)
  250 PRINT "PRINT TAB() TIME:";TPRINT
  260 PRINT "VDU TIME:";TVDU

The above program simply sets the cursor location to print ABC using the TAB and the VDU 31 command. As can be seen the difference here is that the VDU command requires the ASCII characters to be known up front, but when you know this and perhaps there is only a single char to display it could be just what you need.

Here are the timings:

The difference is small but clear. If you want to plot something on the screen of a known ASCII value then you can use VDU 31…

Check it out on YouTube: