Okay, so I've been working on creating a game like Stick Ninja, or whatever it's called. As of now, it works fine, but there is one segment of code that is EXTREMELY slow.

Code:

:For(I,0,D
:If I /= 0
:Line(6, 6, I+5, iPart(√(D²-(I-1)²))+6, 0
:Line(6, 6, I+6, iPart(√(D²-I²))+6
:If 54 = real(8
:Goto 5
:End

How could I make this faster???

P.S. D is the length of the pole, just in case that helps.
The most obvious error is (1) that you have a Goto in a speed-critical section of code and (2) that you have a Goto inside a For/End loop. Where is Lbl 5?
As Kerm said, if Lbl 5 just comes directly after your loop, then you could just do this:


Code:
:For(I,1,D
:If I /= 0
:Line(6, 6, I+5, int(√(D²-(I-1)²))+6, 0
:Line(6, 6, I+6, int(√(D²-I²))+6
:If 54 != real(8
:End


However, more code is going to have to be provided in order to have a better idea of what is actually going on. Thanks! Smile
Well, the procedure was still slow before I added the If statement. My real issue is with the two Line( commands.
Lbl 5 is near the end of the code... Actually, thinking about it, I could probably just use Stop since Lbl 5 is to terminate the program...
But, anywho, I need help optimizing the lines for speed.
MateoConLechuga wrote:
As Kerm said, if Lbl 5 just comes directly after your loop, then you could just do this:


Code:
:For(I,1,D
:If I /= 0
:Line(6, 6, I+5, int(√(D²-(I-1)²))+6, 0
:Line(6, 6, I+6, int(√(D²-I²))+6
:If 54 != real(8
:End
That would not work (or at least leave leaked memory), because there would be a leftover End that would never be hit. I'd recommend:
Code:
:For(I,1,D
:If I /= 0
:D²-I²
:Line(6, 6, I+5, int(√(Ans-2I+1))+6, 0
:Line(6, 6, I+6, int(√(Ans))+6
:If 54=real(8
:D->I
KermMartian wrote:
MateoConLechuga wrote:
As Kerm said, if Lbl 5 just comes directly after your loop, then you could just do this:


Code:
:For(I,1,D
:If I /= 0
:Line(6, 6, I+5, int(√(D²-(I-1)²))+6, 0
:Line(6, 6, I+6, int(√(D²-I²))+6
:If 54 != real(8
:End
That would not work (or at least leave leaked memory), because there would be a leftover End that would never be hit. I'd recommend:
Code:
:For(I,1,D
:If I /= 0
:D²-I²
:Line(6, 6, I+5, int(√(Ans-2I+1))+6, 0
:Line(6, 6, I+6, int(√(Ans))+6
:If 54=real(8
:D->I

Yes, it would indeed. I can't believe that I wrote something like that... Well, what do you know. My brain obviously went on break for a bit. Razz Anywho, thank you for pointing that out, but Kerm, I think that that above code might not work correctly as well because the first if statement is executing the D²-I², not the Line command. Wink

In addition, int(√(Ans-2I+1)) in this case is not the same as int(√(D²-(I-1)²)).... Notice the location of parentheses. Wink As a possible optimization, you might be able to try:


Code:
:For(I,1,D
:D²-I²
:Line(6, 6, I+5, int(√(Ans+2I+1)+6,0
:Line(6, 6, I+6, 6+int(√(Ans
:If 54=real(8
:D->I
:End


EDIT: Added Kerm's code below! Kerm++, I need to get some rest...

I'm assuming that the /= is not equal to?
MateoConLechuga wrote:
In addition, int(√(Ans-2I+1)) in this case is not the same as int(√(D²-(I-1)²)).... Notice the location of parentheses.
Ah, I see I made a sign error, it should be +2I-1 instead of -2I+1. Wink
int(√(D²-(I-1)²)) = int(√(D²-(I-1)(I-1)))
= int(√(D²-(I²-2I+1)))
= int(√(D²-I²+2I-1)))
= int(√((D²-I²)+2I-1)))
= int(√(Ans+2I-1))) where Ans=(D²-I²)
  
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