Prerequisite: Optimizing Color BASIC series
Here is a simple Color BASIC program that will scale blue box on the screen from small to large then back, going through the scaling processes 100 times.
0 REM scale.bas 10 SW=32/4 ' SCALE WIDTH 20 SH=16/3 ' SCALE HEIGHT 30 SM=.1 ' SCALE INC/DEC 40 S=.5 ' SCALE FACTOR 70 TM=TIMER:FOR Z=1 TO 100 80 W=INT(SW*S) 90 H=INT(SH*S) 100 P=15-INT(W/2)+(7-INT(H/2))*32 110 CLS 120 FOR A=1 TO H 130 PRINT@P+A*32,STRING$(W,175) 140 NEXT A 150 S=S+SM 160 IF H<1 OR H>15 THEN SM=-SM:S=S+(SM*2) 170 NEXT Z 180 ' 60=NTSC 50=PAL 190 PRINT:PRINT (TIMER-TM)/60;"SECONDS"
After this runs, it will report the approximate number of seconds it took. It does this by resetting the TIMER at the start, then printing the current TIMER value divided by 60 (since the CoCo timer is based on the NTSC video interrupt that happens 60 times a second).
NOTE: If you run this on a PAL system, you will need to change the 60 to a 50 in line 190. (edit: thanks, George P., for catching my typo.)
On the Xroar emulator running on my Mac it reports 25.25 seconds.
Your challenge, should you decide to accept it, is to take this code and make it run faster.
- You must leave the basic algorithm intact (the SW, SH, S and SH stuff with all the math). You can rename variables, change the representation of values, speed up PRINTing, etc. but the core program flow should remain the same.
- For bonus points, you are welcome to rewrite the program (in BASIC) to improve upon the algorithm in any way that makes sense, provided it achieves the same results (including the 1 to 100 benchmark loop).
There are some very (very!) simple things that can be done to dramatically improve the speed to his code.
Feel free to share your efforts in the comments. If you post your code, be sure to post the resulting time, too.