Hey, I made an analog clock program in TI-BASIC a while back. I couldn't find it on my computer, so I decided to re-write it. I'm using methods more complicated than I did back then.

When I finished writing the code, the analog clock would start out fine, but then start skipping seconds, which means it was slowing down. Within the program, I was calculating what slope to use for lines for the degree angle of the hands every time the time updated. So I thought to make it faster, just have to pre-generate a list, and then look up what the slope was using the list during the program. In the end it did make it work faster (by faster I mean it took longer to slow down).

I'm told that memory leaks are caused by control structure, and not variables. When I transitioned to pre generating a list, I decided to make a bunch of gotos rather than having to re-write code, since there's no copy paste in the editor. (I didn't have access to SourceCoder at the time).

Now I need help sorting out how to keep the program from leaking. Source code is below.

SourceCoder 3 (CLOCK) wrote:
:ZStandard
:ZSquare
:AxesOff
:ClrDraw
:RecallPic 1
:DelVar L3
:84->A
:90->L3(1)
:
:For(O,2,16,1)
:A->L3(O)
:A-6->A
:End
:174->A
:For(O,17,30,1)
:A->L3(O)
:A-6->A
:End
:500->L3(31)
:Goto 6
:
:Lbl 2
:
:
:getTime->L2
:L2(3)->S
:L2(3)->C
:While S=C
:getTime->L1
:L1(1)->H
:L1(2)->Z
:L1(3)->S
:End
:ClrDraw
:RecallPic 1
:4->G
:Lbl 3
:G-1->G
:If G=3
:Then
:L1(G)->Q
:9->F
:End
:If G=2
:Then
:6->F
:L1(G)->Q
:End
:If G=1
:Then
:4->F
:If H>12
:Then
:H-12->P
:Else
:H->P
:End
:Goto 7
:End
:
:
:Goto 5
:End
:
:Lbl 6
:
:0->K
:0->B
:DelVar L4
:
:
:
:For(I,1,59,1)
:K+1->K
:If K=12
:Then
:0->K
:B+1->B
:End
:B->L4(I)
:
:
:End
:
:Goto 2
:Lbl 7
:If Z!=0
:Then
:L4(Z)->J
:Else
:0->J
:End
:
:If P*5!=60
:Then
:P*5->N
:Else
:0->N
:End
:N+J->Q
:
:
:Lbl 5
:
:If Q>30
:Then
:Q-30->T
:~1->R
:Else
:Q->T
:1->R
:End
:
:
:
:L3(T+1)->D
:If D=90 or D=500
:Then
:If D=90
:Then
:If R=~1
:Then
:0->V
:~1*F->W
:Goto 1
:Else
:0->V
:F->W
:Goto 1
:End
:Else
:If R=~1
:Then
:0->V
:F->W
:Goto 1
:Else
:0->V
:~1*F->W
:Goto 1
:End
:End
:End
:
:
:tan(D)->M
:
:R*((M*F)/sqrt(M^^2+1))->W
:If W=0
:Then
:If R=~1
:Then
:~1*F->V
:Goto 1
:Else
:F->V
:Goto 1
:End
:End
:W/M->V
:Goto 4
:Lbl 1
:
:Goto 4
:
:
:
:Lbl 4
:Line(0,0,V,W)
:
:If G=1
:Then
:Goto 2
:End
:Goto 3


Any ideas?
Lots of ideas, and your code can be significantly cleaned up. Let's take this a little at a time: can you first explain section-by-section how you intend the code to work? Discussing that will likely already lead you toward ways to have fewer special cases you need to handle in your code, which seems to be why your conditionals end up getting so deep and complicated. One tip right off the top of my head: using sine and cosine to compute the end coordinates of the clock hands is the easiest way to do it, and that seems to be missing in your code.
Never, ever call a Goto from an If:Then statement. There's a memory reason for it, you'll eventually slow down enough and get an ERR:MEMORY error.


Code:

:If G=1
:Then
:Goto 2
:End

Can be easily cleaned up to be

Code:
:If G=1
:Goto 2

You won't leave an If:Then statement this way as well. This works by checking if G=1, if it's true it continues if it's false it skips the next line (unless it's a Then, then it looks for an Else command).
Indeed, that was one of the things we were discussing on IRC. Memory-leaking constructs can almost always be deconstructed into Repeat, While, or For loops, or at least be rearranged to set some flag variable that will cause the Goto to be called once all of the conditional block Ends have been passed.
Back to what Kerm said earlier, sin and cos would definitely be a major help in speeding up the process. You could just store the entire program into one while loop, get the time, and then use the current time to find the angles, and then use the angle to figure out the placement of the hand.

Also, here are some random optimizations for future use: Smile


Code:

P*5->N Could Be 5P->N

0->K
0->B
DelVar L4 Could Be

DelVar L4DelVar KDelVar B

L1(1)->H Could Be L1(1->H

~1*F->V Could Be ~F->V


Good Luck! Smile
  
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.

» Go to Registration page
Page 1 of 1
» 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

 

Advertisement