[TI-BASIC] Help with Optimization

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

User avatar
JoostinOnline
Regular Member
Posts: 133
Joined: Wed 11 Jul, 2007 10:42 pm
Location: Behind You

Post by JoostinOnline »

Use "IF" when the condition is more likely to be true, and "IF THEN" when it is most likely to be false. However, avoid using either if you can use formulas instead.
"Macs are the Perfect Computers", said the Perfect Idiot.
Image
Testing for:
Vera
Goplat
New Member
Posts: 12
Joined: Mon 16 Jul, 2007 2:46 pm

Post by Goplat »

Actually, plain If is normally faster even for false conditions. The exception is when it's the only thing in a For loop, like this:

Code: Select all

:For(I,1,100
:If 0
:
:End
This causes a slowdown because temporaries don't get deleted. You can work around the bug by adding something to the loop, or by adding a closing parenthesis to the For (which makes the For loop's body start at the newline after the parenthesis, rather than at the next line, so it effectively adds a blank line at the beginning of the loop). With the workaround, the plain If will be faster than the equivalent If:Then.
User avatar
JoostinOnline
Regular Member
Posts: 133
Joined: Wed 11 Jul, 2007 10:42 pm
Location: Behind You

Post by JoostinOnline »

Forgive me, but I really don't think that is correct.
"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 »

i always thought that if then was the best choise for speed because if is slight faster if correct but significantly slower if false.
User avatar
kalan_vod
Calc King
Posts: 2932
Joined: Sat 18 Dec, 2004 6:46 am
Contact:

Post by kalan_vod »

Code: Select all

In almost each TI-BASIC guide, they tell you to use or not to use the If: Then: End 
statements for reasons of speed, space or simply because it looks better, which leads to 
total confusion.


Format                            Bars        Pixels      Total  
If 0: (empty line)                 253          3          2027  
If 1: (empty line)                  13          4           108  
If 0: Then: (empty line): End       13          6           110  
If 1: Then: (empty line): End       14          7           119  

Try not to use If: Statement, because it is very slow, especially when the conditional is 
often false. If: Then: End is to be preferred because it allows you more than one line of
code ot be executed. 

Now, let's have the party going on for (If :Then :End) versus (If :End :If :end)


Format                                                      Bars        Pixels      Total
If 1: Then: End: If 0: Then: End                             24           2          194
If 1: Then: Else: End                                        15           3          123
If 0: Then: Else: End                                        15           3          123
If 1: Then: (empty line): (empty line): Else: End            16           2          130
If 0: Then: (empty line): (empty line): Else: End            15           5          125
If 1: Then: Else: (empty line): (empty line): End            15           5          125
If 0: Then: Else: (empty line): (empty line): End            16           2          130

So If you have multiple statements that are opposite that can be written as an
If: Then: Else: End command, then do it as it saves bytes and that it goes much faster
Goplat
New Member
Posts: 12
Joined: Mon 16 Jul, 2007 2:46 pm

Post by Goplat »

The If 0 is only slow if you trigger the bug where the temporary equvar doesn't get deleted. Even then, it is not a reason to go to If:Then, because you can easily work around the bug, and it will be faster.

This took 7.7s on my calculator

Code: Select all

:For(I,1,1000)
:If 0
:
:End
This took 8.0s

Code: Select all

:For(I,1,1000
:If 0
:Then
:End
:End
So much for that myth that If:Then is faster. Only use If:Then when you actually need it.
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

How much of the difference is in the parsing of "Then" and "End" though?

anyway, I never meant that one was faster than the other (though they cannot be the same), I merely meant that "IF"/"IF THEN" is faster/slower than itself with a different condition. Which is certainly true.

If at all possible, try never to use IF (nor GOTO either) especially in For loops (the combination of both inside a For loop to escape it is rather silly anyway - why not build the condition into the For itself?)
jimmothy
New Member
Posts: 50
Joined: Sat 09 Dec, 2006 2:13 am

Post by jimmothy »

Goplat wrote:The If 0 is only slow if you trigger the bug where the temporary equvar doesn't get deleted. Even then, it is not a reason to go to If:Then, because you can easily work around the bug, and it will be faster.

This took 7.7s on my calculator

Code: Select all

:For(I,1,1000)
:If 0
:
:End
This took 8.0s

Code: Select all

:For(I,1,1000
:If 0
:Then
:End
:End
So much for that myth that If:Then is faster. Only use If:Then when you actually need it.
im fairly sure i read that if is faster than if:then only in for loops but w/e
Post Reply