TIB: Optimizing tip
[Prev][Next][Index][Thread]
TIB: Optimizing tip
Which do you thinks was smaller?
Which was faster?
Here are the programs I tested:
asf() asd()
Prgm Prgm
Local a,b Local a,b
{10,-1}->b {10,-1}->b
Pause "start" Pause "start"
For a,1,100 For a,1,100
b[mod(a,2)+1]->xc b[mod(a,2)+1]->xc
b[mod(a,2)+1]->yc b[mod(a,2)+1]->yc
if xc<0:0->xc when(xc<0,0,when(xc>10,10,xc))->xc
if xc>10:10->xc when(yc<0,0,when(yc>10,10,yc))->yc
if yc<0:0->yc EndFor
if yc>10:10->yc Disp "done"
EndFor EndPrgm
Disp "done"
EndPrgm
Using "when(" statements not only saves space, but makes the program
faster.
The program that uses the "if" statements takes up 179 bytes and took
9.48 sec to execute.
The program that uses the "when(" statements takes up 165 bytes and took
8.48 sec to execute.
The "when(" statements won by a full second and used 14 bytes less than
the "if"'s.
I would suggest on the TI-92 when you are doing simple if statements such
as:
if xc<0:0->xc
if xc>10:10->xc
if yc<0:0->yc
if yc>10:10->yc
instead use:
when(xc<0,0,when(xc>10,10,xc))->xc
when(yc<0,0,when(yc>10,10,yc))->yc
So no if's and's or but's, use when's whenever (puns intended :-P)
possible. Don't forget when's do return values, but that doesn't mean you
can't execute code inside the when, just make a function to put in the
when. Example: when(value>10,functrue(),value)->value; where functrue is
the name of the function you wish to call.
One more thing, when there is redundant code, just make a macro
(subroutine) in your program
by using the "define" statement on a local variable. Look it up on page
389...
Have fun and optimize,
Michael Van Der Kolk
mikev@iserv.net
http://www.iserv.net/~mikev
Follow-Ups: