CodeWalrus

Development => Calculators => Calculator News, Coding, Help & Talk => Topic started by: SiphonicSugar on November 21, 2015, 04:33:06 PM

Title: Code Optimization for Math Program
Post by: SiphonicSugar on November 21, 2015, 04:33:06 PM
Could anyone help me optimize this TI-Basic code for my math program?  It is really slow.  For an example, if I press [1] on the keyboard, it takes FOREVER to display the number 1 on the screen.  PLEASE HAAAALP!

ClrHome
ClrDraw
AxesOff
0->Xmin
94->Xmax
0->Ymin
62->Ymax
0->Z
""->Str0
While 1
Text(0,1,Str0)
getKey->K
If K=92:Then
"1"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=93:Then
"2"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=94:Then
"3"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=82:Then
"4"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=83:Then
"5"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=84:Then
"6"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=72:Then
"7"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=73:Then
"8"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=74:Then
"9"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=102:Then
"0"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=103:Then
"."->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=104:Then
"~"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=95:Then
"+"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:"+"->Str0:1->Z:End
End
If K=85:Then
"-"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=75:Then
"*"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=65:Then
"/"->Str1
If Z=1:Str0+Str1->Str0
If Z=0:Then:Str1->Str0:1->Z:End
End
If K=105:Then
If Z=1:Then:Text(6,1,expr(Str0)):""->Str0:0->Z:End
End
End
Title: Re: Code Optimization for Math Program
Post by: Excelseo on November 21, 2015, 10:04:14 PM
Okay, let me tell you this.
This is one serious Rube Goldberg machine of a program. It doesn't need to be on the graphscreen (that just makes it harder), it doesn't really need to use getkey (input would work just fine) and you're just making it really hard on yourself, whether that was your intention or not.

If it needs to be on the graphscreen and use getkey, there's not much I know how to do to help, except for minimally increasing efficiency.
Otherwise, here's a simple program that does the same thing.

ClrHome
clear the graphscreen
Input("",Str1
prompt the user for an input without any question text
expr(Str1
store the expression of Str1 into Ans so it displays at the end of the program instead of "Done"

And there you have your entire program. If you need it to be on the graphscreen and using getkey, sorry about that. But here's a 3-line equivalent to which you can add if you'd like.

Hope this helps.
Title: Re: Code Optimization for Math Program
Post by: SiphonicSugar on November 22, 2015, 02:59:00 PM
Thanks for the help!  :D

But I wanted it to be on the graph screen so I would not have to use the massive home screen text, and also, since my intention was to make this program like the TI-92 OS, I was going to add F1-F5 options that would display a menu on the graph screen that would have small text.
Title: Re: Code Optimization for Math Program
Post by: Excelseo on November 24, 2015, 05:47:22 PM
Finally got around to making a proper optimization.
The program is still not by any means fast (there is still about a half-second delay between input and output on my Ti-84 plus), but it's likely faster than the original.

ClrHome
AxesOff
0->Xmin
Xmin->Ymin
1->[triangle]X
[triangle]X->[triangle]Y
" ->Str1
DelVar KRepeat K=45 or K=105
getKey->K
1+6iPart(.1Ans)+10fPart(.1Ans
Str1+sub("[20 spaces]X[10 spaces]-1sin(cos(tan(^ 2 ()/ log(789* ln(456-  123+  0.~ ",Ans,1->Str1
length(Str1
sub(Str1,1,Ans-(sub(Str1,Ans,1)=" ->Str1
sub(Ans,1,length(Ans)-(K=23->Str1
[line to clear any text currently written]
[line to write Str1 in location just cleared]
End
sub(Str1,2,length(Str1,2,length(Str1)-1->Str1
If K[not equal]45:Pause expr(Str1
Title: Re: Code Optimization for Math Program
Post by: Dream of Omnimaga on November 25, 2015, 12:38:30 AM
I did not have much time to compare both code, but I definitively notice that your new code looks less repetitive and much smaller. I'm glad that it's faster, because in some occasions, optimizing for size leads to slower speed. >.<