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 106 users online: 10 members, 73 guests and 23 bots.
Members: hellninjas, HOMER-16, legodude, LuxenD, shundra9, worldspace.
Bots: Spinn3r (2), Magpie Crawler (2), Googlebot (19).
RSS & Social Media
SAX
You must log in to view the SAX chat widget
    » Goto page 1, 2  Next
» View previous topic :: View next topic  
Author Message
xXEpicxXXxFailXx


Expert


Joined: 21 Mar 2010
Posts: 567
Location: The 1930s.

Posted: 02 May 2010 03:49:37 pm    Post subject: Optimize BASIC physics module

Another noob question, this one is about about speeding up my code.

This is my version of the physics code on the omnimaga forums, with added friction and collision. The only problem is because there are so many If statements, it is too slow to be worth adding onto it. Can anyone of you pro's optimize this up for me, or at least tell me how?


Code:

:ZStandard
:ZInteger
:AxesOff
:ClrHome
:DelVar XDelVar ZDelVar ADelVar B
:1→I
:Vertical -45
:Vertical 45
:Horizontal 30
:Horizontal -30
:While 1
:DelVar K
:getKey→K
:Pt-Off(X,Z,2
:X+A→X
:Z+B→Z
:Pt-On(X,Z,2
:A+(K=26)-(K=24)→A
:If K=0 and A<0
:A+I→A
:If K=0 and A>0
:A-I→A
:B+(K=25)-(K=34)→B
:If K=0 and B<0
:B+I→B
:If K=0 and B>0
:B-I→B
:If Z≥27
:Then
:DelVar B
:Z-1→Z
:B-1→B
:DelVar K
:Horizontal 30
:End
:If X≥44
:Then
:0→A
:X-1→X
:A-1→A
:0→K
:Vertical 45
:End
:If Z≤{-}27
:Then
:0→B
:Z+1→Z
:B+1→B
:0→K
:Horizontal -30
:End
:If X≤-44
:Then
:0→A
:X+1→X
:A+1→A
:0→K
:Horizontal -30
:End
:End


(For what the variables are, check this forum by clicking here)

Besides that, Variable I is friction.

A screenshot of my program is supplied here.

Yeah, REALLY slow.
_________________
-Sirrico
Back to top
Will_W


Expert


Joined: 06 Feb 2009
Posts: 529

Posted: 02 May 2010 04:06:41 pm    Post subject:


Code:
:ZStandard
:ZInteger
:AxesOff
:ClrHome
:DelVar XDelVar ZDelVar ADelVar B
:1→I
:Vertical -45
:Vertical 45
:Horizontal 30
:Horizontal -30
:While 1
:getKey→K
:Pt-Off(X,Z,2
:X+A→X
:Z+B→Z
:Pt-On(X,Z,2
:A+(K=26)-(K=24→A
:B+(K=25)-(K=34→B
:If K=0:Then
:A+I((A<0)-(A>0→A
:B+I((B<0)-(B>0→B
:End
:If Z≥27
:Then 
:Z-1→Z
:{-}1→B
:DelVar K
:Horizontal 30
:End
:If X≥44
:Then
:X-1→X
:{-}1→A
:DelVar K
:Vertical 45
:End
:If Z≤{-}27
:Then
:Z+1→Z
:1→B
:DelVar K
:Horizontal -30
:End
:If X≤-44
:Then 
:X+1→X
:1→A
:DelVar K
:Horizontal -30
:End
:End

_________________
TI smote the Beta Testers for their insolence and rested. SynThesIs 14
He doesn't have to wake up, you know. I have a laser, and we have a garbage disposal. - Pintsize

What does the GUI say about his fill level? IT'S OVER 9000!!!
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55751
Location: Earth, Sol, Milky Way

Posted: 02 May 2010 04:15:45 pm    Post subject:

Nice Will_W, now I'll see what I can do in terms of consolidating all the If blocks.

I also believe there's a Horizontal in your code that's supposed to be a Vertical.


Code:
:ZStandard
:ZInteger
:AxesOff
:ClrHome
:DelVar XDelVar ZDelVar ADelVar B
:1→I
:Vertical -45
:Vertical 45
:Horizontal 30
:Horizontal -30
:While 1
:getKey→K
:Pt-Off(X,Z,2
:X+A→X
:Z+B→Z
:Pt-On(X,Z,2
:A+(K=26)-(K=24→A
:B+(K=25)-(K=34→B
:If K=0:Then
:A+I((A<0)-(A>0→A
:B+I((B<0)-(B>0→B
:End
:Z-(Z≥27)+(Z≤{-}27→Z
:(Z<27 and Z>-27)B-(Z≥27)+(Z≤{-}27→B
:X-(X≥44)+X≤-44→X
:(X<44 and X>-44)A-(X≥44)+(X≤{-}44→A
:K(X<44 and X>{-}44 and Z<27 and Z>{-}27→K
:If Z≥27
:Horizontal 30
:If X≥44
:Vertical 45
:If Z≤{-}27
:Horizontal {-}30
:If X≤-44
:Vertical {-}45
:End

_________________


Back to top
xXEpicxXXxFailXx


Expert


Joined: 21 Mar 2010
Posts: 567
Location: The 1930s.

Posted: 02 May 2010 04:23:39 pm    Post subject:

Thanks guys, I'm still in a craze that that program can be shortened that much. That was my first attempt ever at a physics module, and now I have an idea of the professional way to do it.
_________________
-Sirrico
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55751
Location: Earth, Sol, Milky Way

Posted: 02 May 2010 04:36:47 pm    Post subject:

xXEpicxXXxFailXx wrote:
Thanks guys, I'm still in a craze that that program can be shortened that much. That was my first attempt ever at a physics module, and now I have an idea of the professional way to do it.
Our pleasure, please keep throwing code at us to optimize so we can show you more optimization techniques. Smile
_________________


Back to top
elfprince13


OVER NINE THOUSAND!


Joined: 23 May 2005
Posts: 10232
Location: A galaxy far far away......

Posted: 02 May 2010 08:07:53 pm    Post subject:

My guess is more optimizations like the ones I just did can be done with the conditional increment/decrements down further, but I didn't feel like working them out in my head. If you understand what I did with the first few, you should have a good headstart on the others. Also, I restored a missing parenthesis. The not() I added in place of Kerm's =0 is a good general optimization, but if you want speed over size, you may want to do some tests comparing them, because my other optimizations help on size, but use more complicated operators than just +, -, and comparisons.


Code:
:ZStandard
:ZInteger
:AxesOff
:ClrHome
:DelVar XDelVar ZDelVar ADelVar B
:1→I
:Vertical -45
:Vertical 45
:Horizontal 30
:Horizontal -30
:While 1
:getKey→K
:Pt-Off(X,Z,2
:X+A→X
:Z+B→Z
:Pt-On(X,Z,2
:25-K
:A+Ans(1=abs(Ans→A
:B+(K=25)-(K=34→B
:If not(K
:Then
:A+I(A/(abs(A)+not(A→A
:B+I(B/(abs(B)+not(B→B
:End
:Z-(Z≥27)+(Z≤{-}27→Z
:(Z<27 and Z>-27)B-(Z≥27)+(Z≤{-}27→B
:X-(X≥44)+(X≤-44→X
:(X<44 and X>-44)A-(X≥44)+(X≤{-}44→A
:K(X<44 and X>{-}44 and Z<27 and Z>{-}27→K
:If Z≥27
:Horizontal 30
:If X≥44
:Vertical 45
:If Z≤{-}27
:Horizontal {-}30
:If X≤-44
:Vertical {-}45
:End

_________________
StickFigure Graphic Productions || VSHI: Vermont Sustainable Heating Initiative


Back to top
xXEpicxXXxFailXx


Expert


Joined: 21 Mar 2010
Posts: 567
Location: The 1930s.

Posted: 02 May 2010 11:14:42 pm    Post subject:

Elfprince13, after exporting all the files and testing them, I believe yours was the fastest, however the friction effect was lost (the box never slows when you let go) and the left and right keys are inverted, which was easy enough to fix. Where would I include the friction effect on yours?

Edit: Are these effects from the unfinished increments?
_________________
-Sirrico
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55751
Location: Earth, Sol, Milky Way

Posted: 02 May 2010 11:23:35 pm    Post subject:

xXEpicxXXxFailXx wrote:
Elfprince13, after exporting all the files and testing them, I believe yours was the fastest,[...]
I should hope so, since his was the third in three successive and increasingly-good optimizations, each examining a different aspect of optimization. Smile

xXEpicxXXxFailXx wrote:
[...]however the friction effect was lost (the box never slows when you let go) and the left and right keys are inverted, which was easy enough to fix. Where would I include the friction effect on yours?

Edit: Are these effects from the unfinished increments?
I believe elfprince has a bug, and five lines should be changed to:


Code:
:If not(K
:Then
:A-I(A/(abs(A)+not(A→A
:B-I(B/(abs(B)+not(B→B
:End

_________________


Back to top
elfprince13


OVER NINE THOUSAND!


Joined: 23 May 2005
Posts: 10232
Location: A galaxy far far away......

Posted: 02 May 2010 11:29:07 pm    Post subject:

Quote:
and the left and right keys are inverted, which was easy enough to fix.

presumably you just replaced the 25-K with K-25?

KermMartian wrote:
I believe elfprince has a bug, and five lines should be changed to:


Code:
:If not(K
:Then
:A-I(A/(abs(A)+not(A→A
:B-I(B/(abs(B)+not(B→B
:End


Aww, did I mess up the signedness again? Also, calc84manic pointed out that

Code:
abs(A)+not(A
and
Code:
abs(B)+not(B
could be optimized to
Code:
abs(A+not(A
and
Code:
abs(B+not(B
thereby saving 2 more bytes.
_________________
StickFigure Graphic Productions || VSHI: Vermont Sustainable Heating Initiative


Back to top
xXEpicxXXxFailXx


Expert


Joined: 21 Mar 2010
Posts: 567
Location: The 1930s.

Posted: 02 May 2010 11:35:33 pm    Post subject:

elfprince13 wrote:

presumably you just replaced the 25-K with K-25?


Yup, that was the first thing I noticed when I reviewed the revised code.

elfprince13 wrote:
Also, calc84manic pointed out that
Code:
abs(A)+not(A
and
Code:
abs(B)+not(B
could be optimized to
Code:
abs(A+not(A
and
Code:
abs(B+not(B
thereby saving 2 more bytes.


Just tested it, all functionality has been restored to the original with increased speed, thanks.
_________________
-Sirrico
Back to top
elfprince13


OVER NINE THOUSAND!


Joined: 23 May 2005
Posts: 10232
Location: A galaxy far far away......

Posted: 03 May 2010 02:31:12 am    Post subject:

Further updates


Code:
:If not(K
:Then
:A-I(A/abs(A+not(A→A
:B-I(B/abs(B+not(B→B
:End
:Z-(Z≥27)+(Z≤{-}27→Z
:(abs(Z) < 27)B-(Z≥27)+(Z≤{-}27→B
:X-(X≥44)+(X≤{-}44→X
:(abs(X) < 44)A-(X≥44)+(X≤{-}44→A
:K(abs(X)<44 and 27>abs(Z→K

(note that another parenthesis has been stripped from the A-I(...→A and B-I(...→B lines)

Also, Will suggested that this might be faster (and definitely the same size)

Code:
:If not(K
:Then
:A-I(1-2(A<0→A
:B-I(1-2(B<0→B
:End
:Z-(Z≥27)+(Z≤{-}27→Z
:(abs(Z) < 27)B-(Z≥27)+(Z≤{-}27→B
:X-(X≥44)+(X≤{-}44→X
:(abs(X) < 44)A-(X≥44)+(X≤{-}44→A
:K(abs(X)<44 and 27>abs(Z→K


Which could be cut further to produce:

Code:
:If not(K
:Then
:A-I+2I(A<0→A
:B-I+2I(B<0→B
:End
:Z-(Z≥27)+(Z≤{-}27→Z
:(abs(Z) < 27)B-(Z≥27)+(Z≤{-}27→B
:X-(X≥44)+(X≤{-}44→X
:(abs(X) < 44)A-(X≥44)+(X≤{-}44→A
:K(abs(X)<44 and 27>abs(Z→K


One more pass to save time on branching cuts us by a few more bytes (S and T can be any variables that you aren't using).

Code:

:Inot(K→T
:A-T+2T(A<0→A
:B-T+2T(B<0→B
:Z-(Z≥27)+(Z≤{-}27→Z
:27>abs(Z→S
:SB-(Z≥27)+(Z≤{-}27→B
:X-(X≥44)+(X≤{-}44→X
:44>abs(X→T
:TA-(X≥44)+(X≤{-}44→A
:K(T and S→K


One more trick, to save a hunk of code. At the top of your program (after AxesOff) insert the following:

Code:
:FnOff
:"(X≤{-}44)-(X≥44→Y1
:"(Z≤{-}27)-(Z≥27→Y2

This allows one final optimization which extends a bit farther.


Code:

:Inot(K→T
:A-T+2T(A<0→A
:B-T+2T(B<0→B
:Z+Y2→Z
:27>abs(Z→S
:SB+Y2→B
:X+Y1→X
:44>abs(X→T
:TA+Y1→A
:K(T and S→K
:If 27≥abs(Z
:Horizontal {-}Y2 30
:If 44≥abs(X
:Vertical {-}Y1 45
:End

_________________
StickFigure Graphic Productions || VSHI: Vermont Sustainable Heating Initiative


Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55751
Location: Earth, Sol, Milky Way

Posted: 03 May 2010 09:55:53 am    Post subject:

I believe that this code would/will produce a divide-by-zero error:


Code:
:A-I(A/abs(A+not(A→A
:B-I(B/abs(B+not(B→B


But now I see my mistake. Nice optimizations there, elfprince. Smile
_________________


Back to top
elfprince13


OVER NINE THOUSAND!


Joined: 23 May 2005
Posts: 10232
Location: A galaxy far far away......

Posted: 03 May 2010 10:11:00 am    Post subject:

KermMartian wrote:
I believe that this code would/will produce a divide-by-zero error:


Code:
:A-I(A/abs(A+not(A→A
:B-I(B/abs(B+not(B→B


But now I see my mistake. Nice optimizations there, elfprince. Smile


I thought it was pretty sneaky, but Will's ended up providing more ammo for optimization. Now that I'm awake I can see that could actually be trimmed by another two parentheses, but I think his will still be faster, since it doesn't do division. On the other hand, if storing to a variable is at all expensive, this one may still have the upper hand, I'm not sure. It's worth playing with though. So I'm gonna write a test driver to time it.

Code:
:A-IA/abs(A+not(A→A
:B-IB/abs(B+not(B→B


[edit]
Alright, so interestingly enough that version (which still branches) is the same size as Will's version and over 3000 loops comes out slightly ahead for speed (and WAY ahead when some actually presses a key). I don't understand how TI's parser works, but that first result absolutely bizarre. So I guess that makes one more optimization.
Change this

Code:

:Inot(K→T
:A-T+2T(A<0→A
:B-T+2T(B<0→B

back to this



Code:
:If not(K
:Then
:A-IA/abs(A+not(A→A
:B-IB/abs(B+not(B→B
:End

_________________
StickFigure Graphic Productions || VSHI: Vermont Sustainable Heating Initiative


Back to top
Will_W


Expert


Joined: 06 Feb 2009
Posts: 529

Posted: 03 May 2010 10:55:15 am    Post subject:

Perhaps because of the extra VAT lookup with
Code:
Inot(K->T

Actually, 2 lookups, one for I and one for T.

Try this and see if no division + branching is faster:

Code:
:If not(K
:Then
:A-I(1-2(A<0→A
:B-I(1-2(B<0→B
:End

_________________
TI smote the Beta Testers for their insolence and rested. SynThesIs 14
He doesn't have to wake up, you know. I have a laser, and we have a garbage disposal. - Pintsize

What does the GUI say about his fill level? IT'S OVER 9000!!!
Back to top
elfprince13


OVER NINE THOUSAND!


Joined: 23 May 2005
Posts: 10232
Location: A galaxy far far away......

Posted: 03 May 2010 11:13:36 am    Post subject:

Quote:


Code:
:If not(K
:Then
:A-I(1-2(A<0→A
:B-I(1-2(B<0→B
:End


So I just did

Code:
:If not(K
:Then
:A-I+2I(A<0→A
:B-I+2I(B<0→B
:End

Which is 2 bytes smaller than yours, but still 2 bytes larger than mine, and it's still slower during a keypress, and wayyy slower without. Your version (+4 bytes) is again slower on the keypress scenario, and still slower without (the faster than the +2 byte version above). I've been measuring seconds over 3000 loop iterations, and if the results are within 1 second, I run them again to see if it stays that way or if it was a timing blip. So, I think it's definitely safe to say that the division version is faster.
_________________
StickFigure Graphic Productions || VSHI: Vermont Sustainable Heating Initiative


Back to top
Will_W


Expert


Joined: 06 Feb 2009
Posts: 529

Posted: 03 May 2010 05:32:22 pm    Post subject:

The only difference in speed during the keypress would have to arise from parsing the extra characters, since every byte parsed requires at least 1 bcall, and it probably fetches each byte until it find an End to match the Then.
_________________
TI smote the Beta Testers for their insolence and rested. SynThesIs 14
He doesn't have to wake up, you know. I have a laser, and we have a garbage disposal. - Pintsize

What does the GUI say about his fill level? IT'S OVER 9000!!!
Back to top
elfprince13


OVER NINE THOUSAND!


Joined: 23 May 2005
Posts: 10232
Location: A galaxy far far away......

Posted: 03 May 2010 05:34:53 pm    Post subject:

Will_W wrote:
The only difference in speed during the keypress would have to arise from parsing the extra characters, since every byte parsed requires at least 1 bcall, and it probably fetches each byte until it find an End to match the Then.


So then it seems that in most cases shaving bytes is more important than operation complexity?
_________________
StickFigure Graphic Productions || VSHI: Vermont Sustainable Heating Initiative


Back to top
Will_W


Expert


Joined: 06 Feb 2009
Posts: 529

Posted: 03 May 2010 05:42:51 pm    Post subject:

Would be interesting to look into it in other cases. When skipping a block, it also has to check for nested blocks to make sure it has the right End for the Then
_________________
TI smote the Beta Testers for their insolence and rested. SynThesIs 14
He doesn't have to wake up, you know. I have a laser, and we have a garbage disposal. - Pintsize

What does the GUI say about his fill level? IT'S OVER 9000!!!
Back to top
Weregoose


Cemetech Expert


Joined: 23 Oct 2009
Posts: 463

Posted: 04 May 2010 05:50:20 pm    Post subject:

U=Friction
V=Bounce
I=Late


Code:
FnOff
ClrHome
ClrDraw
GridOff
AxesOff
PlotsOff
ZStandard
ZInteger
DelVar ADelVar BDelVar CDelVar D.9→U
.6→V
Vertical -45
Vertical 45
Horizontal -30
Horizontal 30
Repeat 0
Pt-On(A,B,2
A→X:B→Y
getKey→K
U(C+(Ans=26)-(Ans=24→C
A+Ans→A
If 43<abs(Ans
Then
43tanh({E}9Ans→A
-VC→C
End
U(D+(K=25)-(K=34→D
B+Ans→B
If 28<abs(Ans
Then
28tanh({E}9Ans→B
-VD→D
End
Pt-Off(X,Y,2
End

_________________
Common Errors in English · How To Ask Questions The Smart Way


Last edited by Weregoose on 04 May 2010 06:02:10 pm; edited 1 time in total
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55751
Location: Earth, Sol, Milky Way

Posted: 04 May 2010 05:53:48 pm    Post subject:

As always, Weregoose, a deep bow to your insane optimization skills.
_________________


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
    » Goto page 1, 2  Next
» View previous topic :: View next topic  
Page 1 of 2 » 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.053945 seconds.