
Optimizing Code
Now, you all know, or at least should know, that TI-BASIC is slow. Every TI-BASIC programmer wants to make their
programs as fast as possible. But how? Assembly libraries? Yes. Ditching TI-BASIC programming and using C/assembly? Yes.
But there's also another way, that everybody is always trying to do - optimizing their code. This means coding efficiently
and reducing repetiton. For example, consider that following TI-BASIC code snippet for the Voyage 200:
:While health>1
:Output row,10,"X"
:Output 1,column,"W"
:getKey()->g
:If g=337 Then:Output 1,column," ":column-4 -> column
:ElseIf g=440 Then:Output 1,column," ":column+4 -> column
:EndIf
:Output row,10," "
:row+1 -> row
:If row=20:1 -> row
:EndWhile
The snippet above could be used to move a character around the screen depending on what key the user pressed.
Let's take a look at the code. First, you might notice that the loop must go through the part where it says:
:If g=337 Then:Output 1,column," ":column-4 -> column
:ElseIf g=440 Then:Output 1,column," ":column+4 -> column
:EndIf
...even if the user didn't even pres a key. That is something we want to avoid. It slows the program down unnecessarily, which is the opposite of what we want.
We change this by making it have to go to a label if the certain keys are pressed:
:While health>1
:Output row,10,"X"
:Output 1,column,"W"
:getKey()->g
:If abs(g-338.5)=1.5:Goto move
:Output row,10," "
:row+1 -> row
:If row=20:1 -> row
:EndWhile
:Lbl move
...
Why did I take the absolute value of something though? If you noticed, the two key codes we are testing for are 337 and 440.
The average of the two is 338.5. So we don't have to check for -1.5 and 1.5, we take the absolute value of the expression.
Now using our new method, we have now made the code more efficient.
But we aren't done yet - we still have to make the code that tells the processor what to do after going to the label "move".
Here's what we'll do:
:Lbl dispChar
:Output 1,column,"W"
:While health>1
:Output row,10,"X"
:getKey()->g
:If abs(g-338.5)=1.5:Goto move
:Output row,10," "
:row+1 -> row
:If row=20:1 -> row
:EndWhile
:Lbl move
:Output 1,column," "
:column+4 -> column
:If g=337:column-8 -> column
:Goto dispChar
What just happenned? If you look at the code, you might notice that it seems like I never tested to see if the user pressed
the key with the code 440. I actually did, and here's how. Let's say I did press the key with a code of 440. It would
first be stored to "g". Then, the program will narrow the options down to either either 337 or 440 with this:
:If abs(g-338.5)=1.5:Goto move
After that, the current location of "W" is cleared. Then, "column"'s value is increased by 4. If the key with a code 337
was pressed, then, "column"'s value would be decreased by 8 - essentially deceasing it by 4 because it just finished being
increased by 4. Since we're pretending I pressed the key with a code of 440, the value of the variable "column" would remain 4. The program then goes to the label "dispChar" and displays "W" at it's new position. This is more efficient because the program doesn't have to display "W" every single time it goes through the loop, even if the location of "W" didn't change.
Now, you should hopefully have a good understanding how optimize code to make your programs more efficient =). Maybe, if you're up to it, you could try to optimize the code even more - I bet it's possible.
programs as fast as possible. But how? Assembly libraries? Yes. Ditching TI-BASIC programming and using C/assembly? Yes.
But there's also another way, that everybody is always trying to do - optimizing their code. This means coding efficiently
and reducing repetiton. For example, consider that following TI-BASIC code snippet for the Voyage 200:
Let's take a look at the code. First, you might notice that the loop must go through the part where it says:
...even if the user didn't even pres a key. That is something we want to avoid. It slows the program down unnecessarily, which is the opposite of what we want.
We change this by making it have to go to a label if the certain keys are pressed:
But we aren't done yet - we still have to make the code that tells the processor what to do after going to the label "move".
Here's what we'll do:
Now, you should hopefully have a good understanding how optimize code to make your programs more efficient =). Maybe, if you're up to it, you could try to optimize the code even more - I bet it's possible.