[TI-BASIC] Help with Optimization

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Moose
New Member
Posts: 29
Joined: Sun 02 Sep, 2007 6:48 am
Location: Shhhhh, they'll hear me!

[TI-BASIC] Help with Optimization

Post by Moose »

Hello, me again :)

I was just wondering if anyone could help me optimize this quick game I made. I tried every trick I can think of, but it still runs too slow for
too much enjoyment. Its just a quick one, so its very short :wink:

It's called glider, its just a game where you're a glider who flies over hills, and the closer you get to the ground, the more points you get.

Code: Select all

16->dim(L1
Fill(6,L1
6 -> E
5 -> A
0 -> B

Repeat A>L1(1 or K=45
ClrHome

Output(A,1,">
Output(8,1,"SCORE:
Output(8,9,B
For(F,1,16
Output(L1(F,F,"-
End

randInt(1,3 -> D
E+(D=1)-(D=2) -> E

If E>8
8 -> E

If E<2
2 -> E

E -> L(17
For(F,2,17
L1(F -> L1(F-1
End

getkey -> K
A-(K=25)+(K=34 -> A

If A<1
1->A

round((2^A)/10,0)+B -> B

End

There's a closing screen with a score, but that doesn't really matter. :wink:
User avatar
JoostinOnline
Regular Member
Posts: 133
Joined: Wed 11 Jul, 2007 10:42 pm
Location: Behind You

Re: [TI-BASIC] Help with Optimization

Post by JoostinOnline »

Code: Select all

16->dim(L1
Fill(6,L1
DelVarB6 -> E
5 -> A

Repeat A>L1(1 or K=45
ClrHome

Output(A,1,">
Output(8,1,"SCORE:
Output(8,9,B
For(F,1,16
Output(L1(F,F,"-
End

randInt(1,3 -> D
E+(E<8)(D=1)-(E>1)(D=2) -> E

E -> L(17
For(F,2,17
L1(F -> L1(F-1
End

getkey -> K
A-(A>1)(K=25)+(K=34 -> A

B+ipart(2^A/10-> B

End
It would also speed it up alot if you used " " instead of ClrHome to erase where the glider had been, but I'm too lazy to add that in right now :wink: .
burr
New Member
Posts: 4
Joined: Thu 30 Aug, 2007 3:55 am

Re: [TI-BASIC] Help with Optimization

Post by burr »

Code: Select all

16→dim(L1:Fill(6,L1
DelVarB6→E:5→A
Repeat A>L1(1 or K=45
ClrHome
Output(A,1,">
Output(8,1,"SCORE:
Output(8,9,B
For(F,1,16
Output(L1(F),F,"-
End
1+int(3rand→D
max(2,min(8,E+(Ans=1)-(Ans=2→E
Ans→L(17
For(F,2,17
L1(F→L1(F-1
End
getkey→K
A-(Ans=25 and A>1)+(Ans=34→A
B+iPart(.1(2^A→B
End
User avatar
JoostinOnline
Regular Member
Posts: 133
Joined: Wed 11 Jul, 2007 10:42 pm
Location: Behind You

Post by JoostinOnline »

@burr: If he wants speed, then he shouldn't use "and", even if it saves a byte. Also, I think I read somewhere that "/10" is faster than ".1"
"Macs are the Perfect Computers", said the Perfect Idiot.
Image
Testing for:
Vera
burr
New Member
Posts: 4
Joined: Thu 30 Aug, 2007 3:55 am

Post by burr »

JoostinOnline wrote:@burr: If he wants speed, then he shouldn't use "and", even if it saves a byte. Also, I think I read somewhere that "/10" is faster than ".1"
I don't know where you read that, but your assumptions are wrong -- and is faster than implicit multiplication, and multiplication is faster than division. The TI-84+/SE has a built-in timer command that allows you to test the speed of your code, so that's how I know.
User avatar
JoostinOnline
Regular Member
Posts: 133
Joined: Wed 11 Jul, 2007 10:42 pm
Location: Behind You

Post by JoostinOnline »

burr wrote:
JoostinOnline wrote:@burr: If he wants speed, then he shouldn't use "and", even if it saves a byte. Also, I think I read somewhere that "/10" is faster than ".1"
I don't know where you read that, but your assumptions are wrong -- and is faster than implicit multiplication, and multiplication is faster than division. The TI-84+/SE has a built-in timer command that allows you to test the speed of your code, so that's how I know.
I have learned from experience that multiplication is faster than using "and". Its also in many guides such as http://www.ticalc.org/archives/files/fi ... 39433.html :P . However, you were right about ".1" being faster than "/10".
Goplat
New Member
Posts: 12
Joined: Mon 16 Jul, 2007 2:46 pm

Post by Goplat »

I did an experiment in PindurTI to prove that "and" is faster. These numbers are from OS 1.12.

Code: Select all

Input		T states elapsed between ends of lines (breakpoint set at $18:67B1)

:(0)(0		15078
:(0 and 0	13700
:(0)(1		15126
:(0 and 1	13748
:(1)(0		15380
:(1 and 0	14002
:(1)(1		16721
:(1 and 1	13897
There you go - "and" is consistently faster than multiplying. (Edit: I had put the breakpoint in the wrong place so it was only measuring parsing speed, fixed)
Last edited by Goplat on Wed 05 Sep, 2007 2:28 am, edited 1 time in total.
User avatar
JoostinOnline
Regular Member
Posts: 133
Joined: Wed 11 Jul, 2007 10:42 pm
Location: Behind You

Post by JoostinOnline »

I'm just going by all the guides on ticalc.org and personal experience.
"Macs are the Perfect Computers", said the Perfect Idiot.
Image
Testing for:
Vera
jimmothy
New Member
Posts: 50
Joined: Sat 09 Dec, 2006 2:13 am

Post by jimmothy »

idk, it seems it'd best to measure stuff on a real calc. just test to see which takes longer when executed 100 times or something.
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post by driesguldolf »

imo it sounds logic that and is faster, as it only has to check wether any of the arguments is zero, the output is zero. Multiplying as not so simple and thus takes longer.

BUT... have you checked 0*0 instead of (0)(0? Maybe there is a significant difference? Plus and doesn't need braces wich should make it faster & smaller than multiplying.
User avatar
JoostinOnline
Regular Member
Posts: 133
Joined: Wed 11 Jul, 2007 10:42 pm
Location: Behind You

Post by JoostinOnline »

driesguldolf wrote:imo it sounds logic that and is faster, as it only has to check wether any of the arguments is zero, the output is zero. Multiplying as not so simple and thus takes longer.

BUT... have you checked 0*0 instead of (0)(0? Maybe there is a significant difference? Plus and doesn't need braces wich should make it faster & smaller than multiplying.
Yeah, I would think that and would be faster, but I just tested it on my calc and using multiplication saved 3 seconds when counting to 1000. But since its such a small time difference Moose may want to go with "and" to save a byte. I guess it just depends on how long your test will last.
Goplat
New Member
Posts: 12
Joined: Mon 16 Jul, 2007 2:46 pm

Post by Goplat »

driesguldolf wrote:BUT... have you checked 0*0 instead of (0)(0? Maybe there is a significant difference? Plus and doesn't need braces wich should make it faster & smaller than multiplying.
Of course 0*0 would be faster than (0)(0, but that's irrelevant. To multiply conditionals you would need the parenthesis because the comparison operators have a lower precedence than multiplication.
JoostinOnline wrote:I just tested it on my calc and using multiplication saved 3 seconds when counting to 1000.
What was the code you used? If you didn't put parenthesis around the terms, the comparison is meaningless.

I just tested it on my calc; the loop with multiplication took 7.0s while the loop with "and" took 6.7s.
User avatar
JoostinOnline
Regular Member
Posts: 133
Joined: Wed 11 Jul, 2007 10:42 pm
Location: Behind You

Post by JoostinOnline »

I don't remember the code, but I know I used parenthesis. I don't really see why it matters though, since I already said that he should go ahead and use your advice (I had no idea that the time difference was so small)
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post by driesguldolf »

Goplat wrote:
driesguldolf wrote:BUT... have you checked 0*0 instead of (0)(0? Maybe there is a significant difference? Plus and doesn't need braces wich should make it faster & smaller than multiplying.
Of course 0*0 would be faster than (0)(0, but that's irrelevant. To multiply conditionals you would need the parenthesis because the comparison operators have a lower precedence than multiplication.
You may want to do "If AnsP" instead of "If Ans and P" where it does matter, but in this case go for the "and" method (saves a byte and it's slightly faster)
it's not entirely 0*0 :mrgreen: but wether and is faster than implicit multiplication or maybe explicit multiplication is the fastest?

And the slight difference makes me think that to calculate "and" it effectively multiplies them instead of ORing all the bytes together :S
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

Shouldn't "AND" AND the numbers together?

"AND" doesn't even need to do that, it could be faster to check one parameter first, if it's false just go to ELSE, if it's true check the other parameter and go to ELSE if it's true
But that's too complicated for TI ;)
although multiplying goes a bit far imo

note also that "IF" and "IF THEN" are very different, one of them is faster on FALSE, the other is faster on TRUE, which one though I don't remember..
Post Reply