SB BBC @ GBR Oldie hints/tips #072-074 Hints and tips from the archives of Wakefield BBC Micro User Group... 72. Top 10 speed techniques ~~~~~~~~~~~~~~~~~~~~~~~ Here is a list of techniques to increase the speed of your programs, courtesy of Bob Horne. Some are fairly well known, others may be new to you. The amount of improvement can vary from dramatic to marginal, but small improvements here and there can add up to substantial increases in speed. You should use the TIME facility to measure which parts of the program are slowest, and to assess the effect of using these techniques. You should initially concentrate your attentions on statements inside loops, whether they be FOR-NEXT loops, REPEAT-UNTIL or IF-THEN-GOTO loops, as this is where small delays tend to add up. ie. pay special attention to lines which are executed most often. By all means do break these rules if it improves clarity without slowing things down, so use your common sense! 1. Use integer variables, (especially the resident A%-Z%), and integer arrays where possible, including loop-counter variables. (ie. FOR A%=1 TO 10.) 2. When graphics are drawn with a FOR-NEXT loop, use the largest STEP size the Mode resolution will allow. (ie. 4 vertically, and 2, 4 or 8 horizontally.) 3. Miss off the loop-counter variables in NEXT statements. (ie. use NEXT rather than NEXT A%.) 4. Ensure that all unnecessary calculations are done outside loops. (eg. X*3*PI/2 inside a loop is slower than X*p, where p=3*PI/2 before the loop is entered.) Use REMs where they will be executed, then put them on the end of an existing line, rather than separately. eg b=RAD(b):REM convert to radians. 5. Don't put REMs or blank lines where they will be executed often; even though they don't actually do anything, they will slow things down. They are harmless if the program jumps round them, but don't go putting in GOTOs specially, as that's just as bad. If you must use REMs where they will be executed, then put them on the end of an existing line rather than separately. Eg: 10 b=RAD(b):REM convert to radians 6. Use multistatement lines; the fewer line numbers that are involved, the faster the program works. 7. Procedures/Functions may sometimes be faster than GOSUBs, (but you never use GOSUBS anyway, do you?) 8. Use short Procedure/Function names, try to have them all start with a different letter, and put the DEF of the Procedures called most often before the others, (ie. at a lower line number). 9. Use short variable names, and try to start them with different letters, as for Procedures. 10.FOR-NEXT loops are faster than REPEAT-UNTIL or IF-THEN-GOTO loops. 73. Interlacing in modes 0-6 ~~~~~~~~~~~~~~~~~~~~~~~~ Turning the interlacing off can produce a much steadier picture. This is done using the *TV command, which is only partly explained on page 23 of the User Guide. The full syntax is *TV A,B , where A determines how many lines up or down the display is moved, and B determines whether the i/l is turned on or off. Thus, to leave the picture as it is, but to turn the i/l off, you use *TV0,1. To move the picture down one line as well, you use *TV255,1. To move the display, but keep the i/l on, you would use *TV255,0 , which can be shortened to the familiar *TV255. To restore the display to normal, ie. i/l on and no shift, you use *TV0,0, which can be shortened to just *TV. Don't forget that the commands will have no effect until you change Mode, and that the interlacing always stays on in Mode 7. 74. Interlacing in mode 7 ~~~~~~~~~~~~~~~~~~~~~ Having just told you that you cannot turn the interlacing off in Mode 7, here is a way of doing just that! This idea was passed on by Dave G4IAU, who thinks that it may have originated from Belfast University. It eliminates the classic Mode 7 'shake', which can be especially bad on some samples of Microvitec monitors. It is a three-part VDU23 statement. The first part turns the i/l off, and the second halves the number of scan lines per character to correct for the lack of interlacing. I stuck the final part on to halve the number of scan lines before the cursor starts, otherwise it gets lost. The commands alter registers 8,9 and 10 of the 6845 CRT controller, and this is explained on pages 364-367 of the Advanced User Guide. The command takes effect immediately, and is reset by a Mode change. It is very effective on block graphics and double-height characters, but you lose the rounding on normal-height characters, with interesting results! I/L off:- VDU23;8,144,0;0;0;23;9,9,0;0;0;23;10,105,0;0;0; I/L on :- VDU23;8,147,0;0;0;23;9,18,0;0;0;23;10,114,0;0;0; 73 Rick G4BLT @ GB7WRG