Piecewise Expressions vs. If Then

So I'm drawing a box that will highlight specific parts of the screen. I decided to go overboard with piecewise expressions to save line space so the getKey will update faster. I was wondering if this hurts me in the long run. Below is the code I made with variables added on above it. Some of the project has been removed to keep what I'm making in the dark.

Also the only optimized code is the piecewise expression part, the getKey if then sequences above are for testing. Plus there is more to the figure than just 9 boxes like this program will do, but all calculations using piecewise expressions must be made so it will account for more faces.

Code:
```ClrDraw:ClrHome:BackgroundOn White:AxesOff 0->Xmin 264->Xmax ~164->Ymin 0->Ymax 138->D:~88->E:1->F:5->G D->Q:E->R:F->S:G->T //Draws Starting Point Box Line(D,E,D+28,E,LtGray Line(D,E,D,E-28,LtGray Line(D+28,E,D+28,E-28,LtGray Line(D,E-28,D+28,E-28,LtGray Repeat A=105 getKey->A If A!=105 and A!=0:Then //Determining Key Input If A=24:Then If G!=1 or G!=4 or G!=7:Then D-28->Q G-1->T End If G=1 or G=4 or G=7:Then D+56->Q G+2->T End:End If A=25:Then If G>3:Then E+28->R G-3->T End If G<4:Then E-56->R G+6->T End:End If A=26:Then If G!=3 or G!=6 or G!=9:Then D+28->Q G+1->T End If G=3 or G=6 or G=9:Then D-56->Q G-2->T End:End If A=34:Then If G<7:Then E-28->R G+3->T End If G>6:Then E+56->R G-6->T End:End //Highlight pieces //Fills in Last Box If D!=Q or E!=R or F!=S or G!=T:Then //Up Line(D,E,(D+28-(15*(F=2)-1*(F=2 and G=2 or F=2 and G=5 or F=2 and G=8))),(E-(13*(F=2)+1*(F=2 and G=2 or F=2 and G=5 or F=2 and G=8))),Black //Left Line(D,E,(D+(13*(F=3)+1*(F=3 and G>3 and G<7))),(E-28+(15*(F=3)-1*(F=3 and G>3 and G<7))),Black //Right Line((D+28-(15*(F=2)-1*(F=2 and G=2 or F=2 and G=5 or F=2 and G=8))),(E-(13*(F=2)+1*(F=2 and G=2 or F=2 and G=5 or F=2 or G=8))),(D+28-(15*(F=2)-1*(F=2 and G=2 or F=2 and G=5 or F=2 and G=8))),(E-28+(15*(F=3)-1*(F=3 and G>3 and G<7))-(13*(F=2)+1*(F=2 and G=2 or F=2 and G=5 or F=2 or G=8))),Black //Down Line((D+(13*(F=3)+1*(F=3 and G>3 and G<7))),(E-28+(15*(F=3)-1*(F=3 and G>3 and G<7))),(D+28-(15*(F=2)-1*(F=2 and G=2 or F=2 and G=5 or F=2 and G=8))+(13*(F=3)+1*(F=3 and G>3 and G<7))),(E-28+(15*(F=3)-1*(F=3 and G>3 and G<7))-(13*(F=2)+1*(F=2 and G=2 or F=2 and G=5 or F=2 and G=8))),Black Q->D:R->E:S->F:T->G End //Creates New Box //Up Line(D,E,(D+28-(15*(F=2)-1*(F=2 and G=2 or F=2 and G=5 or F=2 and G=8))),(E-(13*(F=2)+1*(F=2 and G=2 or F=2 and G=5 or F=2 and G=8))),LtGray //Left Line(D,E,(D+(13*(F=3)+1*(F=3 and G>3 and G<7))),(E-28+(15*(F=3)-1*(F=3 and G>3 and G<7))),LtGray //Right Line((D+28-(15*(F=2)-1*(F=2 and G=2 or F=2 and G=5 or F=2 and G=8))),(E-(13*(F=2)+1*(F=2 and G=2 or F=2 and G=5 or F=2 or G=8))),(D+28-(15*(F=2)-1*(F=2 and G=2 or F=2 and G=5 or F=2 and G=8))),(E-28+(15*(F=3)-1*(F=3 and G>3 and G<7))-(13*(F=2)+1*(F=2 and G=2 or F=2 and G=5 or F=2 or G=8))),LtGray //Down Line((D+(13*(F=3)+1*(F=3 and G>3 and G<7))),(E-28+(15*(F=3)-1*(F=3 and G>3 and G<7))),(D+28-(15*(F=2)-1*(F=2 and G=2 or F=2 and G=5 or F=2 and G=8))+(13*(F=3)+1*(F=3 and G>3 and G<7))),(E-28+(15*(F=3)-1*(F=3 and G>3 and G<7))-(13*(F=2)+1*(F=2 and G=2 or F=2 and G=5 or F=2 and G=8))),LtGray End:End```

Code:
```If A A If A A```

Is a little bit slower than

Code:
```If A Then A A End ```

but his time difference is almost unnoticeable. Also, the latter is one byte larger.

Generally, for testing speed, I use

Code:
```For(A,1,100 //Adjust the number of iterations of the For( loop to fit your needs, but mostly 100 is ok. //Code to test here End ```

And then I count how many times that looping line in the top right corner (It is called run indicator) loops around. Of course this is only suitable for simple code chunks, complex algorithms won't work...
The For( loop is explained best with an example: You have a piece of paper and you want to measure its thikness. Take 100 papers and divide their thickness by 100, and here you go.

Ps: The first code took about 2.5 run indicator loops, the second one about 2.
Piecewise expressions are generally faster, but they always execute all of your code. Conditionals are slower, but they skip code when the comditional is false. Therefore, piecewise expressions are faster except in the case where:

* The condition is false a significant amount of the time,
AND
* The code to execute when the condition is true is time-consuming.

Therefore,

Code:
```A+fnInt(X^X,X,1,A)(rand<.999->A A+2(rand<.5->A ```

should not be written as conditionals, but

Code:
```A+fnInt(X^X,X,1,A)(rand<.5->A ```

should.

The best way to time code on an 84+ series calc is:

Code:
```startTmr->T Repeat checkTmr(Ans End For(n,1,[iters]) [routine to test] End checkTmr(T+1 ```

Close the parenthesis on the For loop for consistent timings between If: and If:Then statements.
Thanks for all the help and suggestions guys!

I have put forth some effort to improve my design and now it works great. I decided that I should keep the piecewise expressions but put them in buffer variables before executing any graphical display. Also in the midst of this revamp, I discovered a bug, and I had to sort through 200+ lines of code to fix it. My program is better because of it though.

Heres the buffer code for those of you who want a look.

Code:
```138->D:~88->E:1->F:5->G D->Q:E->R:F->S:G->T (D+28-(15*(F=2)))->W (E-(13*(F=2)))->V (D+(13*(F=3)))->U (E-28+(15*(F=3)))->X (E-28+(15*(F=3))-(13*(F=2)))->Y (W+(13*(F=3)))->Z //Up Line(D,E,W,V,LtGray //Left Line(D,E,U,X,LtGray //Right Line(W,V,W,Y,LtGray //Down Line(U,X,Z,Y,LtGray```

Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

»
» All times are UTC - 5 Hours

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum