Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: Sorunome on July 24, 2013, 11:53:09 am

Title: Fastest/Most Optimized way to move a character around the homescreen
Post by: Sorunome on July 24, 2013, 11:53:09 am
Hey, I was just curious, what is the fastest/most optimized way to move a character around the home screen?
what I currently have is this:
Code: [Select]
ClrHome
1->A
1->B
Repeat C=45
Output(B,A,"*")
Repeat C
getkey->C
End
Output(B,A," ")
max(min(A-(C=24)+(C=26),16),1)->A
max(min(B-(C=25)+(C=34),8),1)->B
End
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: dinosteven on July 24, 2013, 12:17:51 pm
You can replace variables with Ans in a few places.
Code: [Select]
ClrHome
1→A
Ans→B
Repeat C=45
Output(B,A,"*")
Repeat Ans
getKey→C
End
Output(B,A," ")
max(min(A-(Ans=24)+(Ans=26),16),1)→A
max(min(B-(C=25)+(C=34),8),1)→B
End
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: DJ Omnimaga on July 24, 2013, 12:44:29 pm
You can also remove some of the closing parentheses but that won't affect speed at all, sadly.
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Sorunome on July 24, 2013, 12:45:45 pm
You can replace variables with Ans in a few places.
why didn't i think of ans x.x
You can also remove some of the closing parentheses but that won't affect speed at all, sadly.
i know that, but idk why i put 'em in in this prog....
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Xeda112358 on July 24, 2013, 12:47:46 pm
Personally, I would organise it as:
Code: [Select]
ClrHome
1→A
Ans→B
Repeat C=45
Output(B,A,"*
Repeat Ans
getKey→C
End
Output(B,A,"
max(1,min(16,A-(Ans=24)+(Ans=26→A
max(1,min(8,B-(C=25)+(C=34→B
End
But that is just  size optimsation with probably a negligible speed boost. That is pretty close to my typical routine :P
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: dinosteven on July 24, 2013, 05:25:30 pm
Oh, and you can also reorganize it so that the getkey loop is last. That way, you can replace the Repeat C=45 with Repeat Ans=45.
Code: [Select]
ClrHome
DelVarC1→A
Ans→B
Repeat Ans=45
Output(B,A,"
max(1,min(16,A-(Ans=24)+(Ans=26→A
max(1,min(8,B-(C=25)+(C=34→B
Output(B,A,"*
Repeat Ans
getKey→C
End
End
But that requires a DelVar of C in case C somehow had a value of 26 or 34... x.x
EDIT:
And let's not forget the ultimate tool for the guy who desperately wants to use Ans, and Ans only... LISTS! I'm not sure if a list in Ans is faster than variables accessed normally... Probably the list is slower... anyone want to test this code? My calc is bricked...
Code: [Select]
ClrHome
{1,1,0
Repeat 45=Ans(3
Output(Ans(1),Ans(2),"*
Repeat Ans(3
{Ans(1),Ans(2),getKey
End
Output(Ans(1),Ans(2),"
{max(1,min(16,Ans(1)-(Ans(3)=24)+(Ans(3)=26))),max(1,min(8,Ans(2)-(Ans(3)=25)+(Ans(3)=34))),Ans(3
End
Oh gahwd, this is messy. It's definitely not faster lol.
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Sorunome on July 24, 2013, 05:26:34 pm
um, why is there a DelVar C, you don't need that.

Also, that is some pretty nice optimizing :D
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: dinosteven on July 24, 2013, 05:50:10 pm
um, why is there a DelVar C, you don't need that.
But that requires a DelVar of C in case C somehow had a value of 26 or 34... x.x
If C=26 or C=34 before launching the program, then it'll start off in the wrong place, having already moved. lol, doesn't really matter, but just in case.
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Joshuasm32 on July 24, 2013, 09:11:35 pm
I still have never really figured out how Repeat works...  I do this:

ClrHome
DelVar K
1->X
1->Z
While K!=45
getkey->K
If K!=0:Then
ClrHome
X+(K=26 and X<16)-(K=24 and X>0)=->X
Z+(K=34 and Z>0)-(K=25 and Z<8)->Z
Output(X,Z,"X
End
End
ClrHome

It is less efficient and probably slower...  D:  Can someone help me understand how to use Repeat?
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: blue_bear_94 on July 24, 2013, 09:17:44 pm
Repeat executes the inner body at least once, because it checks whether its condition is true after running each iteration of the body. While doesn't make this guarantee.
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Streetwalrus on July 25, 2013, 01:35:56 am
Repeat executes the inner body at least once, because it checks whether its condition is true after running each iteration of the body. While doesn't make this guarantee...
... as it checks the condition first. Also Repeat executes until the condition turns true and While until it turns false. ;)
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Sorunome on July 25, 2013, 02:48:48 am
um, why is there a DelVar C, you don't need that.
But that requires a DelVar of C in case C somehow had a value of 26 or 34... x.x
If C=26 or C=34 before launching the program, then it'll start off in the wrong place, having already moved. lol, doesn't really matter, but just in case.
But in TI-Basic a loop is alwys entered at least once (I thought), so even if C is 45 in the beginning it'll enter the loop and'll be overwritten by getKey->C
Also, just tested it and i'm right, it enteres the repeat loop once in the beginning
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Xeda112358 on July 25, 2013, 08:10:02 am
Yes, as was said before, Repeat works like this:
Code: [Select]
Lbl 1
<code code code>
If K≠45
Goto 1
While works like this:
Code: [Select]
Lbl 1
If K=45
Then
<code code code>
Goto 1
End
I hope that helps a little. I personally prefer Repeat loops over While loops in most cases.
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: dinosteven on July 25, 2013, 08:35:01 am
um, why is there a DelVar C, you don't need that.
But that requires a DelVar of C in case C somehow had a value of 26 or 34... x.x
If C=26 or C=34 before launching the program, then it'll start off in the wrong place, having already moved. lol, doesn't really matter, but just in case.
But in TI-Basic a loop is alwys entered at least once (I thought), so even if C is 45 in the beginning it'll enter the loop and'll be overwritten by getKey->C
Also, just tested it and i'm right, it enteres the repeat loop once in the beginning
No, I didn't mean that it will quit early if C=45. I meant C=26 or C=34. Do 26->C or 34->C before executing my code (without the DelVar). I'm almost positive that the * will NOT be in the upper left corner, it will be shifted right or down.
...and after testing, 26->C doesn't do this, as my code changes A with Ans, which is initialized as 1. But do a 34->C before executing my code. The * will start out at 2,1 rather than 1,1. That's what I was saying, not that it would quit.
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Sorunome on July 25, 2013, 08:37:33 am
No, again same thing (just tried it out), as xeda pointed out with lables.
You enter the inner loop at least once, the keypress is stored an there you go, C is again 0 until you hit a key.
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: MGOS on July 25, 2013, 08:45:42 am
But C is evaluated before the getkey.

Code: [Select]
Repeat Ans=45
Output(B,A,"
max(1,min(16,A-(Ans=24)+(Ans=26→A
max(1,min(8,B-(C=25)+(C=34→B  //C is evaluated here! If C is 25 or 34, the thing will move
Output(B,A,"*
Repeat Ans
getKey→C     //and here C is being reset
End
End

That means the first time it may happen that the thing moves without a key being pressed because C was not cleared before. It's unlikely because quitting the loop sets C to 45, but if there was a different part of the program that uses C, it may happen (or the program was quit with on before).
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Sorunome on July 25, 2013, 08:46:57 am
oh, right, i forgot that optimization.....i was still at the one where the inner loop was before evaluating.
Thanks for clarifying MGOS

EDIT: wouldn't it be quicker to do Ans->C then instead of DelVar C?
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: dinosteven on July 25, 2013, 12:57:28 pm
Oh, so we were talking about completely different codes lol.
I was interested in whether Ans would be faster than DelVar or not.
Code: [Select]
For(A,1,500
DelVar CEnd
vs
Code: [Select]
For(A,1,500
Ans->C
End
Anyone wanna time this? I would do it, but my calc is bricked...
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Soulthym on July 26, 2013, 07:43:15 am
The first code takes about 3,2secs whereas the second takes 5,7 secs on my calc, so it would be a great optimisation to use "Delvar C" I think
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Sorunome on July 26, 2013, 07:45:15 am
Coudln't we also optimize it to Delvar A,B,C then?
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Soulthym on July 26, 2013, 08:16:55 am
I think so Sorunome
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Hayleia on July 26, 2013, 08:51:56 am
Coudln't we also optimize it to Delvar A,B,C then?
I think so Sorunome
Nope. ERR:Syntax.
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: MGOS on July 26, 2013, 09:06:34 am
You could do
Code: [Select]
DelVar ADelVar BDelVar CThat's the same size (since you don't need new lines in between), but be aware that the homescreen coordinates start at 1,1 in TI-Basic instead of 0,0.
another
Code: [Select]
Delvar AA+1->A would be slower again slower than just
Code: [Select]
1->A so it wouldn't be an optimization.
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Sorunome on July 26, 2013, 09:31:11 am
Oh, I forgot that part x.x
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Hayleia on July 26, 2013, 09:35:50 am
You could do
Code: [Select]
DelVar ADelVar BDelVar CThat's the same size (since you don't need new lines in between)
Nope, "DelVar " is a two-byte token and "," is a one-byte token.

Also, I understand that you want to optimize speed-wise what's inside the loop to get the fastest movement, but the initialization (what is before the loop) should be optimized for size, shouldn't it ?
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: dinosteven on July 27, 2013, 01:07:51 am
Actually, you CAN do the DelVars. The min() and max() reset it, remember? All we need to do is move the erasing Output to the end of the loop, so that it doesn't give an error for out of bounds coordinates.
Like so:
Code: [Select]
DelVar ADelVarBDelVarCClrHome //I don't think commas work for DelVars.
Repeat Ans=45
max(1,min(16,A-(Ans=24)+(Ans=26→A
max(1,min(8,B-(C=25)+(C=34→B
Output(B,A,"*
Repeat Ans
getKey→C
End
Output(B,A,"
End
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Hayleia on July 27, 2013, 02:15:10 am
Actually, you CAN do the DelVars.
Yeah, my "Nope" was not a "Nope, it doesn't work", but a "Nope, it doesn't take the same size" since the DelVar always take one more byte than the period.
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: dinosteven on July 27, 2013, 08:50:26 am
Oh, I was responding to MGOS about the DelVarA vs the 1->A.
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: dinosteven on July 28, 2013, 04:01:16 am
And further, you can remove the DelVar C because the maximum the numbers can become is 1, and the minimum is -1, but is overwritten in the bound check.
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Sorunome on July 28, 2013, 07:39:41 am
What again?
You need the DelVar C otherwise the thing can move right/down when first looping.
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: dinosteven on July 28, 2013, 09:54:07 am
Ah, but with the most recent code, with your optimization of using DelVar ADelVar B instead of 1->A, A and B are now 0. It would've caused some problems with that first erasing output, but I moved that to the bottom. Now the first line in the loop is the min(max(stuff)), which will move it to 1. And even if C=34 or something, max(1,0+1) = 1.
Code: [Select]
DelVar ADelVar BClrHome
Repeat Ans=45
max(1,min(16,A-(Ans=24)+(Ans=26→A // If Ans=24, max(1,min(16,0-1))=1. If Ans=26, max(1,min(16,0+1))=1. If otherwise, max(1,min(16,0))=1 still. Yay.
max(1,min(8,B-(C=25)+(C=34→B //Same applies for C. The max(1,X) guarantees it, and putting A and B as 0 with DelVar makes it so that the maximum it can be from the beginning is 1, which is fine. :)
Output(B,A,"*
Repeat Ans
getKey→C
End
Output(B,A,"   //This would've caused some problems - it would've given out of bounds. But I moved it to the end.
End
:) Test it!
Title: Re: Fastest/Most Optimized way to move a character around the homescreen
Post by: Sorunome on July 28, 2013, 09:56:27 am
oooooooh, i get it now, that is pretty clever! :D