* WalrusIRC

You need to have 5 posts and not be part of restricted usergroups in order to use the WalrusIRC embedded shoutbox. However, you can also access our IRC channel called #CodeWalrus via EFnet.

Author Topic: Code Optimization for Math Program  (Read 645 times)

0 Members and 1 Guest are viewing this topic.

Offline SiphonicSugar

  • Full User
  • Join Date: Jun 2015
  • Location: Tennessee, United States
  • Posts: 249
  • Post Rating Ratio: +1/-1
  • Inspiration
    • siphonicsugar
    • @SiphonicSugar
    • SiphonicS
    • View Profile
  • Gender: Male
Code Optimization for Math Program
« 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!

Code: [Select]
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


  • Calculators owned: TI-89, TI-84 Plus, TI-92, TI-84 Plus C SE, and TI-Nspire CAS with Touchpad
I'm just trying to grab some inspiration. :P

Offline Excelseo

  • New User
  • Join Date: Nov 2015
  • Location:
  • Posts: 4
  • Post Rating Ratio: +0/-0
    • View Profile
Re: Code Optimization for Math Program
« Reply #1 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.

Offline SiphonicSugar

  • Full User
  • Join Date: Jun 2015
  • Location: Tennessee, United States
  • Posts: 249
  • Post Rating Ratio: +1/-1
  • Inspiration
    • siphonicsugar
    • @SiphonicSugar
    • SiphonicS
    • View Profile
  • Gender: Male
Re: Code Optimization for Math Program
« Reply #2 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.
  • Calculators owned: TI-89, TI-84 Plus, TI-92, TI-84 Plus C SE, and TI-Nspire CAS with Touchpad
I'm just trying to grab some inspiration. :P

Offline Excelseo

  • New User
  • Join Date: Nov 2015
  • Location:
  • Posts: 4
  • Post Rating Ratio: +0/-0
    • View Profile
Re: Code Optimization for Math Program
« Reply #3 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

Online DJ Omnimaga

  • Omni founder & CW co-founder
  • CodeWalrus Staff
  • Super User
  • Forum Maintenance
  • Original 5
  • CodeWalrus Supporter
  • *
  • Topic Management
  • Join Date: Nov 2014
  • Location: Quebec, Canada
  • Posts: 17887
  • Post Rating Ratio: +89/-4
    • dj_omnimaga
    • DJOmnimaga.music
    • @DJOmnimaga
    • dj_omnimaga
    • @DJOmnimaga
    • /u/DJ_Omnimaga
    • DJOmnimaga
    • 112/11286
    • @djomnimaga
    • @DJOmnimaga
    • View Profile
    • DJ Omnimaga music store
  • Gender: Male
Re: Code Optimization for Math Program
« Reply #4 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. >.<
  • Calculators owned: TI-73, TI-80 (broken), TI-81, TI-82, TI-83, TI-83+ (broken), TI-83+ (broken), TI-83+SE (broken), TI-84+, TI-84+CSE, TI-84+CE, TI-85, TI-86, TI-89T, TI-92, TI-Nspire, TI-Nspire CX (semi-broken), HP 39gII, HP Prime, Casio fx-7000G, fx-7400G+, fx-7700GE, fx-9750G+, fx-9750GII, fx-9860G, cfx-9850G, FX-1.0+, fx-CG10, fx-CP400
  • Consoles, mobile devices and vintage computers owned: Samsung i5510, Nexus 5, Atari 2600, Lynx, SMS, Game Gear, Genesis, Dreamcast, NES, SNES, N64, GCN, Wii, Wii U, GBA, DS, 3DS, PS2, PS3, PS4, PSP, PSVita, XBox 360, XBOne

Bandcamp|Reverbnation|Facebook|Youtube|Twitter
Retired Omnimaga admin (2001-11) and editor (2012-14)
??? ??? ??? ???

 


You can also use the following HTML or bulletin board code to share it on your page or forum signature!


Also do not forget to check our affiliates below.
Planet Casio TI-Planet Calc.news BroniesQC BosaikNet Velocity Games