| Author |
Message |
|
zeldaking
Power User

Joined: 31 Jul 2011 Posts: 470 Location: Utah
|
Posted: 09 May 2012 10:41:09 pm Post subject: Ti-Basic Optimization |
|
|
Recently I have been teaching a kid at school Ti-Basic. I am now teaching him about optimizing. I made a simple 'game' in 96 bytes but wondering if I could make it smaller. The 'game' moves a '*' around without letting it leave the homescreen boundaries. Yeah I know very simple. Here is what I have:
Code:
:1->A:1->B
:Repeat K=45
:getkey->K
:ClrHome
:Output(B,A,"*
:A+(K=26 and A!=16)-(K=24 and A!=1)->A
:B+(K=34 and B!=8)-(K=25 and B!=1)->B
:End
//!= not equals to
Thanks |
|
| Back to top |
|
|
blue_bear_94
Member

Joined: 20 Apr 2012 Posts: 162
|
Posted: 09 May 2012 10:51:02 pm Post subject: |
|
|
Code:
:1->A:1->B
:Repeat K=45
:getkey->K
:ClrHome
:Output(B,A,"*
:A+(Ans=26 and A!=16)-(Ans=24 and A!=1->A
:B+(K=34 and B!=8)-(K=25 and B!=1->B
:End
//!= not equals to
_________________ Do NOT click any links! |
|
| Back to top |
|
|
TheStorm

NOU!

Joined: 26 Mar 2007 Posts: 2375
|
|
| Back to top |
|
|
zeldaking
Power User

Joined: 31 Jul 2011 Posts: 470 Location: Utah
|
Posted: 09 May 2012 10:54:02 pm Post subject: |
|
|
| Oops sorry, I accidentally wrote those closing parentheses... just going off of memory. looking at my code I didn't have those :p |
|
| Back to top |
|
|
Compynerd255

Power User

Joined: 08 Apr 2011 Posts: 397
|
Posted: 10 May 2012 09:15:29 am Post subject: |
|
|
In floating point operations, does "While K-45" save speed over "Repeat K=45"? I know that it saves time and speed in Axe. _________________ Visit Betafreak Games: http://www.betafreak.com
Help Me Pay for College:
- Sign up for Fastweb through my referal link! |
|
| Back to top |
|
|
Aes_Sedia5

Minor Calculator Deity

Joined: 01 Sep 2011 Posts: 1002 Location: Where Nightmares end and Dreams begin
|
Posted: 10 May 2012 12:08:00 pm Post subject: |
|
|
I know repeat K=45 is pretty standard.. I have seen Repeat in every program that uses sprite movement. as I understood it, they are the same speed. _________________

 |
|
| Back to top |
|
|
turiqwalrus
New Member

Joined: 18 Nov 2010 Posts: 95 Location: Eggplant shopping! :P
|
Posted: 10 May 2012 01:52:00 pm Post subject: |
|
|
| Aes_Sedia5 wrote: | | I know repeat K=45 is pretty standard.. I have seen Repeat in every program that uses sprite movement. as I understood it, they are the same speed. |
In which case Repeat K=45 should be used, as it is more understandable... _________________ This signature is completely empty
except for this
and this
JOIN THE WFCLFY! |
|
| Back to top |
|
|
blue_bear_94
Member

Joined: 20 Apr 2012 Posts: 162
|
|
| Back to top |
|
|
merthsoft
File Archiver

Joined: 09 May 2010 Posts: 2735
|
|
| Back to top |
|
|
builderboy2005

Member

Joined: 05 Aug 2008 Posts: 122
|
Posted: 10 May 2012 02:51:15 pm Post subject: |
|
|
Repeat loops check their condition at the bottom of the loop instead of at the top like While. Consequently a Repeat loop will always execute at least once, which makes it useful for situations like this where we don't care what K was before the loop.
EDIT: Cemetech needs a Ninja notifier like Omni has  |
|
| Back to top |
|
|
zeldaking
Power User

Joined: 31 Jul 2011 Posts: 470 Location: Utah
|
Posted: 10 May 2012 05:44:20 pm Post subject: |
|
|
Yeah the getkey resets K to zero the first time through. I tried while loop, and a for( loop the latter taking the most bytes.
Code:
:for(K,0,45
:getkey->K
:end
|
|
| Back to top |
|
|
KermMartian

Site Admin

Joined: 14 Mar 2005 Posts: 55749 Location: Earth, Sol, Milky Way
|
Posted: 11 May 2012 11:51:10 am Post subject: |
|
|
Indeed, that's a quite clever way to do it, but sadly not sufficiently optimal for all its slyness. I know I'm beating my own drum too much about this, but I feel I wrote up a fairly thorough guide to optimization as Chapter 10 of my book, and I detailed things like the differences between While and Repeat and the proper places to use each. At any rate, I'd say that the code as presented is a quite nice little program, and about as optimized as you'd want to or be able to get for something like this. _________________
 |
|
| Back to top |
|
|
Xeda112358
Power User

Joined: 09 Nov 2010 Posts: 353
|
Posted: 08 Jun 2012 06:59:10 am Post subject: |
|
|
This is a little more optimised:
Code:
:1→A:1→B
:Repeat K=45
:getKey→K
:If Ans ;Added this in so as not to be blinking so much.
:ClrHome
:Output(B,A,"*
:max(1,min(16,A+(K=26)-(K=24→A
:max(1,min(8,B+(K=34)-(K=25→B
:End
|
|
| Back to top |
|
|
KermMartian

Site Admin

Joined: 14 Mar 2005 Posts: 55749 Location: Earth, Sol, Milky Way
|
Posted: 08 Jun 2012 08:03:00 am Post subject: |
|
|
Mmm, that is indeed better. I had held off on the min-max form for the sake of possible confusion, but you're absolutely right that it's smaller. Out of curiosity, have you checked what the relative speed of that is? In this case I'd expect it to be a negligible difference, I suppose. _________________
 |
|
| Back to top |
|
|
Xeda112358
Power User

Joined: 09 Nov 2010 Posts: 353
|
Posted: 08 Jun 2012 08:06:51 am Post subject: |
|
|
| I don't have the timings, but I do imagine it is negligible. min() and max() are fairly fast to compute. |
|
| Back to top |
|
|
zeldaking
Power User

Joined: 31 Jul 2011 Posts: 470 Location: Utah
|
Posted: 08 Jun 2012 09:12:35 am Post subject: |
|
|
| hmmm, I have never used max( or min( before. What is the syntax? |
|
| Back to top |
|
|
KermMartian

Site Admin

Joined: 14 Mar 2005 Posts: 55749 Location: Earth, Sol, Milky Way
|
Posted: 08 Jun 2012 11:37:57 am Post subject: |
|
|
| zeldaking wrote: | | hmmm, I have never used max( or min( before. What is the syntax? | min(X,Y) will find and return the smaller of X and Y; max(X,Y) will find and return the larger of X and Y. If you work with lists instead, min({a,b,c,...z}) or min(L1) will return the smallest value of the list, and symmetrically, max(list) will return the largest value in the list. _________________
 |
|
| Back to top |
|
|
|