[TI-BASIC] Help with Optimization
Moderator: MaxCoderz Staff
- JoostinOnline
- Regular Member
- Posts: 133
- Joined: Wed 11 Jul, 2007 10:42 pm
- Location: Behind You
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: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.
Code: Select all
:For(I,1,100
:If 0
:
:End
- JoostinOnline
- Regular Member
- Posts: 133
- Joined: Wed 11 Jul, 2007 10:42 pm
- Location: Behind You
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
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 calculatorThis took 8.0sSo much for that myth that If:Then is faster. Only use If:Then when you actually need it.
This took 7.7s on my calculator
Code: Select all
:For(I,1,1000)
:If 0
:
:End
Code: Select all
:For(I,1,1000
:If 0
:Then
:End
:End
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
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?)
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?)
im fairly sure i read that if is faster than if:then only in for loops but w/eGoplat 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 calculatorThis took 8.0sCode: Select all
:For(I,1,1000) :If 0 : :End
So much for that myth that If:Then is faster. Only use If:Then when you actually need it.Code: Select all
:For(I,1,1000 :If 0 :Then :End :End