[Coco] Color BASIC optimization challenge – scaling

James Jones jejones3141 at gmail.com
Sat Mar 14 18:00:52 EDT 2020


On Fri, Mar 13, 2020 at 8:30 AM Allen Huffman <alsplace at pobox.com> wrote:

> On Mar 13, 2020, at 12:33 AM, Walter Zambotti <zambotti at iinet.net.au>
> wrote:
> >
> > Try changing the inner loop to remove all calculations like this
> >
> > 115 P2=P+32:H2=P+H*32:BK$=STRING$(W,175)
> > 120 FOR A=P2 TO H2 STEP 32
> > 130 PRINT @A,BK$
> > 140 NEXT A
> >
> > I believe I chopped 7 seconds off the time.
>
> Nicely done! I’ll be sure to include this tip in the follow up article.
>

Yes. The relevant actions (and compiler buzzwords where applicable):

   - strength reduction, for turning a multiply involving the loop control
   variable into an add. (Since that's the only use of the control variable,
   you can just make the control variable work hold the value you want instead
   of adding another variable.
   - Moving calculation that doesn't depend on the loop control variable
   out of the loop, namely the addition of P and evaluating STRING$(W,175).

The loop body is short, so you won't lose much legibility and will cut
interpreter overhead by putting the FOR loop all on one line, and ditching
the explicit mention of the loop control variable on the NEXT statement
will also help some. I suspect that's the majority of the possible speedup
if you're confining yourself to BASIC---that's the innermost loop.


More information about the Coco mailing list