Don't have an account? Register now to chat, post, use our tools, and much more.
Latest Headlines
Online Users
There are 99 users online: 6 members, 70 guests and 23 bots. Members: adriweb, alain, legodude, tifreak8x. Bots: VoilaBot (1), Magpie Crawler (3), Googlebot (13), Googlebot (5), MSN/Bing (1).
RSS & Social Media
SAX
You must log in to view the SAX chat widget
|
| 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
|
|
| Back to top |
|
|
KermMartian

Site Admin

Joined: 14 Mar 2005 Posts: 55736 Location: Earth, Sol, Milky Way
|
|
| 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: 55736 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.  _________________
 |
|
| Back to top |
|
|
elfprince13

OVER NINE THOUSAND!

Joined: 23 May 2005 Posts: 10225 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: 55736 Location: Earth, Sol, Milky Way
|
|
| Back to top |
|
|
elfprince13

OVER NINE THOUSAND!

Joined: 23 May 2005 Posts: 10225 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: 10225 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: 55736 Location: Earth, Sol, Milky Way
|
|
| Back to top |
|
|
elfprince13

OVER NINE THOUSAND!

Joined: 23 May 2005 Posts: 10225 Location: A galaxy far far away......
|
|
| Back to top |
|
|
Will_W

Expert

Joined: 06 Feb 2009 Posts: 529
|
|
| Back to top |
|
|
elfprince13

OVER NINE THOUSAND!

Joined: 23 May 2005 Posts: 10225 Location: A galaxy far far away......
|
|
| 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: 10225 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: 55736 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 |
|
|
|
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
|
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.050522 seconds.
|