CEMETECH
Leading The Way To The Future
Login [Register]
Username:
Password:
Autologin:

Don't have an account? Register now to chat, post, use our tools, and much more.
Latest Headlines
Online Users
There are 125 users online: 3 members, 83 guests and 39 bots.
Members: lboe.
Bots: VoilaBot (4), Spinn3r (1), MSN/Bing (1), Magpie Crawler (4), VoilaBot (9), Googlebot (20).
RSS & Social Media
SAX
You must log in to view the SAX chat widget
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

Posted: 09 May 2012 10:51:38 pm    Post subject: Re: Ti-Basic Optimization


Code:

:1->A:1->B
:Repeat K=45
:ClrHome
:Output(B,A,"*
:getkey->K
: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


Saved two bytes and made a slight speed optimization. I know there is more to be saved but that is what I saw with a quick glance.

Edit: Blue_bear beat me too it, then again I took the time to say what I did.
_________________

"Always code as if the person who will maintain your code is a maniac serial killer that knows where you live" -Unknown

"If you've done something right no one will know that you've done anything at all" -Futurama

"Have a nice day, or not, the choice is yours." Tom Steiner

<Michael_V> or create a Borg collective and call it The 83+
<Michael_V> Lower your slide cases and prepare to be silent linked. Memory clears are futile.
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 Very Happy

except for this
and this
JOIN THE WFCLFY!
Back to top
blue_bear_94


Member


Joined: 20 Apr 2012
Posts: 162

Posted: 10 May 2012 02:47:16 pm    Post subject:

What if K is already 45?
_________________
Do NOT click any links!
Back to top
merthsoft


File Archiver


Joined: 09 May 2010
Posts: 2735

Posted: 10 May 2012 02:51:07 pm    Post subject:

Repeat evaluates the conditional at the end of the loop, rather than the beginning. See http://tibasicdev.wikidot.com/repeat. While does it before, so doing "While K-45" could cause the loop to not run. See http://tibasicdev.wikidot.com/while
_________________
Shaun
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 Laughing
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: 55742
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: 352

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: 55742
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: 352

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: 55742
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
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are GMT - 5 Hours

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© Copyright 2000-2013 Cemetech & Kerm Martian :: Page Execution Time: 0.038398 seconds.