Re: TI85 Concatenating numbers/Numbers -> Strng


[Prev][Next][Index][Thread]

Re: TI85 Concatenating numbers/Numbers -> Strng



Speed up the value to string function? OK here are some steps you can take:


First:  It makes me happy to see you put comments in your program.
However you probably double the execution time of the algorithm with
your comments. So when you get your program is debugged TAKE OUT THE
COMMENTS.
Explanation: When you store a new value into the number variable A, the
calculator just writes the new value and goes on. When you write a new
value to a string, the calculator must move all the other variables in
RAM after the string to make room for the new string (which is almost
always a new length). It also must update all the pointers. In fact, the
optimazation tip I'll give next involves completely eliminating the
sting variables from the main part of your program.


I don't want to type out a whole routine but I think i can explain it.
There is a variable on all the calculators called Ans. I believe from
observing the calcs that the Ans variable is right next to free ram. It
is therefore the fastest variable to change. So here's a bogus algorythm
that will show you how to exploit the answer variable.  DISCLAIMER: Do
not ever, ever, write code like I'll be showing her for school, college
credit, or your job or you will fail and/or get fired.




" "
for(x,3,8
Ans+sub("0123456789",x,1
end
Ans -> Str0


This code will put the string "234567" into Str0 very quickly. Note that
the only variable that changes size is Ans, the fastest variable on the
TI.
( NOte the lack of close parens in the code. This is a great way to make
your programs smaller (they're not nessecary))


Now it gets worse. YOU CANNOT USE THE "STORE" OPERATOR ( -> ) ANYWHERE
IN YOUR ALGORITHM, OR YOU WILL DESTROY YOUR STRING! So if you need to
update the value of a real variable that keeps track of what digit
you're working on, you can't type


mod(C,10) -> C


Here's the workaround:


use a for loop to store stuff in real variables like this


for(C, mod(C,10), -10000000:end


This will change the value of C but not Ans. (Thanks TI) The idea of the
-1000000 is to put a value there that is always less than mod(C,10) so
that the loop always terminates without running. You don't want to loose
speed running an empty FOR loop! (Probably -1 will work fine for this
particular aplication.)


now you're ready to write string programs that don't contain the evil,
slow -> operator. It will be hell to debug them. AND DON'T PUT COMMENTS
IN, OR YOU'LL DESTROY YOUR STRING. Remeber: the ti EVALUATES your
comments; a real compiler on a computer IGNORES them.


********


You know, I feel kinds bad about posting this stuff. I know that at
least one beginner programmer will try to write programs like this and
learn terrible habbits. I programmed in Pascal for years with lots of
comments and READABIBILITY OPTIMAZATIONS, not speed optimazations,
before doing this stuff. I earned the right to write disgusting code
exclusively for myself for the express purpose of increasing speed. If
you're new to programming, please do not try to write programs using
these speed increasing tips. You will end up with programs that you
can't debug. Trust me, I still mess up the ans varible now and then with
a call to a misbehaved sub routine (one that changes Ans). And I NEVER
do these optimizations until I have a working copy of a nicely written
program backed up on my computer. Only AFTER completing a program do I
go back and speed it up.








With  that out of the way, there's one more technique for speed
optimazation: write inline code. What I mean by inline code is you need
to remove all the loops from your program by explicitly writing out the
stuff in the loop over and over. This does little good on an 85 (it
tokenizes and links up stuff before running programs), but on my good
old 81, the speed increase you get from removing loops is significant.
Weigh the benifits of the speed increase against the extra memory loss
from writing out all that stuff. The decision is up to you. I sugesst
that you write the program, test it, change it, and see if the change
was worth it. That's how I learned these "secrets" for increasing speed.
Have fun.




-Jonathan


References: