Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: Happybobjr on June 17, 2010, 07:53:00 pm

Title: Code optimization
Post by: Happybobjr on June 17, 2010, 07:53:00 pm
please help make this code better so i can publish it

Code: [Select]
.EESKETCH
Full
diognosticoff
DispGraph
ClrDraw
0->S->O->P+1->L+9->X->Y+90->A
Text(82,0,A|>Dec
Text(1,0,L|>Dec
Untill G=15
getKey->G
asm(DB00E6036F2600)->I
If I=0:For(J,0,L):Pxl-Off(rand^96,rand^65):End:DispGraph:End
If X<95 and getKey(3):X+1->X:sub(A):End
If Y>7 and getKey(4):Y-1->Y:sub(A):End
If X>0 and getKey(2):X-1->X:sub(A):End
If Y<62 and getKey(1):Y+1->Y:sub(A):End
If G>4
While getKey(29) and (G=29):Text(1,0,L|>Dec):If L>1:L-1->L:Pause 125:End:End
While getKey(21) and (G=21):If L<255:L+1->L:Text(1,0,L|>Dec):Pause 125:End:End
While getKey(10) and (G=10):A+1->A:Text(82,0,A|>Dec):Pause 110:End
While getKey(11) and (G=11):Text(82,0,A|>Dec):If A>1:A-1->A:Pause 110:End:End
If G=13 and getKey(13):StorePic :X->O:Y->P:End
If G=12 and getKey(12):RecallPic :O->X:P->Y:DispGraph:End
If G=56 and getKey(56):ClrDraw:DispGraph:End
If G=14 and getKey(14):DrawInv :DispGraph:End
End
End
Lbl A
Pxl-On(X,Y):DispGraph:Pause A
Return
Normal
Title: Re: Code optimization
Post by: SirCmpwn on June 17, 2010, 07:59:48 pm
Code: [Select]
.EESKETCH
Full
diognosticoff
DispGraph
ClrDraw
0→S→O→P+1→L+9→X→Y+90→A
Text(82,0,A|>Dec
Text(1,0,L|>Dec
Untill G=15
getKey→G
asm(DB00E6036F2600→I
!If I
For(J,0,L
Pxl-Off(rand^96,rand^65
End:DispGraph:End
If X<95 and getKey(3
X+1→X:sub(A
End
If Y>7 and getKey(4
Y-1→Y:sub(A
End
If X>0 and getKey(2
X-1→X:sub(A
End
If Y<62 and getKey(1
Y+1→Y:sub(A
End
If G>4
While G=29 and getKey(29
Text(1,0,L|>Dec
If L>1:L-1→L:Pause 125:End:End
While G=21 and getKey(21
If L<255:L+1→L:Text(1,0,L|>Dec
Pause 125:End:End
While G=10 and getKey(10
A+1→A:Text(82,0,A|>Dec):Pause 110:End
While G=11 and getKey(11
Text(82,0,A|>Dec
If A>1:A-1→A:Pause 110:End:End
If G=13 and getKey(13
StorePic :X→O:Y→P:End
If G=12 and getKey(12
RecallPic :O→X:P→Y:DispGraph:End
If G=56 and getKey(56
ClrDraw:DispGraph:End
If G=14 and getKey(14
DrawInv :DispGraph:End
End
End
Lbl A
Pxl-On(X,Y
DispGraph:Pause A
Title: Re: Code optimization
Post by: Quigibo on June 17, 2010, 08:03:01 pm
There are places where you do +9 and +90 to initialize variables.  Those are not optimizations!  Its only an optimization if the number is between negative 2 and positive 2.

Also there are places of code that look like this:
A+1→A:Text(82,0,A|>Dec)

They can be optimized like this:
Text(82,0,A+1→A|>Dec)
Title: Re: Code optimization
Post by: Magic Banana on June 17, 2010, 08:10:10 pm
Not really optimizations, but in SirCmpwn's code, things like:
If X<95 and getKey(3
should be
If X<95 and (getKey(3)
so that the Parser doesn't throw any errors.
Title: Re: Code optimization
Post by: Quigibo on June 17, 2010, 08:14:04 pm
You don't actually need parenthesis there since direct keys are binary functions that return 0 or 1 always.  And it definately shouldn't throw an error.
Title: Re: Code optimization
Post by: Magic Banana on June 17, 2010, 08:30:25 pm
Really? it always gives me bad symbol when i leave the parentheses from getkey()
Title: Re: Code optimization
Post by: Quigibo on June 17, 2010, 08:37:40 pm
You do need it for getkey(_) itself, but you don't need to do this: (getkey(_)) is what I was referring to.
Title: Re: Code optimization
Post by: calcdude84se on June 17, 2010, 08:38:27 pm
Nope, even w/v0.3.0, leaving the closing parenthesis off of something like getKey(12) and also getKey(0) throws a bad symbol error.
Edit: Quigibo already clarified and was referring to something slightly different than I was. Ninja'd, I suppose.
Title: Re: Code optimization
Post by: Magic Banana on June 17, 2010, 08:47:45 pm
Oh, yeah I know about that. I only put 1 closing parenthesis for the getkey and left the 'and' parentheses open.
Title: Re: Code optimization
Post by: Happybobjr on June 18, 2010, 08:59:41 am
ok i got all that done,
btw you can have things like
if g=26 and getkey(26
it has to be
if g=26 and getkey(26)
Title: Re: Code optimization
Post by: Magic Banana on June 18, 2010, 02:29:14 pm
huh, I thought that there needed to be an opening parenthesis whenever you used 'and' or 'or' in if/while/repeat statements for each extra item after the first.

ex.
if X=9 and (Y=2
if X=9 and (Y=2 or (Z=6
Title: Re: Code optimization
Post by: calc84maniac on June 18, 2010, 02:46:50 pm
huh, I thought that there needed to be an opening parenthesis whenever you used 'and' or 'or' in if/while/repeat statements for each extra item after the first.

ex.
if X=9 and (Y=2
if X=9 and (Y=2 or (Z=6
That's only because you have an operation after the "and". If X or Y or Z, for example, would work fine.

Quigibo, why do you force closing parentheses after getKey(? It really confused me when I got bad symbol errors with that a while back.
Title: Re: Code optimization
Post by: Builderboy on June 18, 2010, 02:52:10 pm
I think its because getKey can only take constants as arguments? 
Title: Re: Code optimization
Post by: nemo on June 18, 2010, 02:52:22 pm
Code: [Select]
If X<95 and getKey(3):X+1->X:sub(A):End
If Y>7 and getKey(4):Y-1->Y:sub(A):End
If X>0 and getKey(2):X-1->X:sub(A):End
If Y<62 and getKey(1):Y+1->Y:sub(A):End

...

Lbl A
Pxl-On(X,Y):DispGraph:Pause A
Return

can be (in axe 3.0)

Code: [Select]
If X<95 and getKey(3)
sub(A,X+1->X,Y)
End
If Y>7 and getKey(4)
sub(A,X,Y-1->Y)
End
If X>0 and getKey(2)
sub(A,X-1->X,Y)
End
If Y<62 and getKey(1)
sub(A,X,Y+1->Y)
End

...

Lbl A
Pxl-On(r1,r2
DispGraph
Pause A
Return
Title: Re: Code optimization
Post by: Magic Banana on June 18, 2010, 03:10:07 pm
So, how exactly does the parameter passing work in 0.3.0? I know you can send up to 3 arguments to a sub, but does it just turn r1 - r6 into normal variables? what about sending r1 - r6 as parameters? I'm still trying to figure out how to use the new features as they weren't really explained in great detail.
Title: Re: Code optimization
Post by: Quigibo on June 18, 2010, 03:10:15 pm
Actually, even though the bottom is cleaner and easier to read, the top is still more optimized I think.  That's because he doesn't need to store to temporary variables to call the subroutine, the original subroutine was specifically made to only draw points from the X,Y variables which is fine for his use.

EDIT: And yes, r1-r6 work just like normal variables.  Anywhere you can use a letter A-Theta you can also use r1-r6, including for loops and storing to them.
Title: Re: Code optimization
Post by: Galandros on June 18, 2010, 04:33:33 pm
But they look very convenient for temporary uses like in functions and loops.
That really should help readability of code. Nice add!
Title: Re: Code optimization
Post by: Magic Banana on June 18, 2010, 08:07:31 pm
Another question about parameters: Can we use constants as parameters in addition to variables? Something like:
Code: [Select]
Sub(SE,X,Y,14or just
Code: [Select]
Sub(BL,9,30,2to store those numbers to the r1-r6? Also, since it's r1-r6, does that mean we can send up to 6 to the sub?
Title: Re: Code optimization
Post by: Quigibo on June 18, 2010, 08:29:21 pm
Not only can you use constants AND variables, you can us ANY expression including other subroutines and asm code.   If you want, you can call something with sub(L,X+2,Str-X*2^5-A,sign{'A'+sub(B)-{L1}}).  Go crazy.

And yes, you can have up to 6 arguments and as few as 0.  Only the temporary variables than need to be passed actually get updated.  You can have subroutines that take a variable number of arguments too.  Like if you make the first parameter be "number of arguments" then you can have the same subroutine function multiple ways with different numbers of arguments.
Title: Re: Code optimization
Post by: DJ Omnimaga on June 18, 2010, 08:48:40 pm
Just don't do stuff like this :P

sub(AA,sub(AB,sub(AC,sub(AD,sub(AE,sub(AF,sub(AG,sub(AH,sub(AI,sub(AJ,sub(AK,sub(AL,sub(AM,sub(AN,sub(AO,sub(AP,sub(AQ,sub(AR,sub(AS,sub(AT,sub(AU,sub(AV,sub(AW,sub(AX,sub(AY,sub(AZ,sub(BA,sub(BB,sub(BC,sub(BD,sub(BE,sub(BF,sub(BG,sub(BH,sub(BI,sub(BJ,sub(BK,sub(BL,sub(BM,sub(BN,sub(BO,sub(BP,sub(BQ,sub(BR,sub(BS,sub(BT,sub(BU,sub(BV,sub(BW,sub(BX,sub(BY,sub(BZ,sub(CA,sub(CB,sub(CC,sub(CD,sub(CE,sub(CF,sub(CG,sub(CH,sub(CI,sub(CJ,sub(CK,sub(CL,sub(CM,sub(CN,sub(CO,sub(CP,sub(CQ,sub(CR,sub(CS,sub(CT,sub(CU,sub(CV,sub(CW,sub(CX,sub(CY,sub(CZ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

...unless that is actual valid code?
Title: Re: Code optimization
Post by: nemo on June 18, 2010, 08:51:52 pm
excuse my french but... mindfuck? i dare someone to have a program with 20 subroutines and the line that DJ just posted only. and it has to do something useful.
Title: Re: Code optimization
Post by: Happybobjr on June 18, 2010, 08:51:56 pm
Just don't do stuff like this :P

sub(AA,sub(AB,sub(AC,sub(AD,sub(AE,sub(AF,sub(AG,sub(AH,sub(AI,sub(AJ,sub(AK,sub(AL,sub(AM,sub(AN,sub(AO,sub(AP,sub(AQ,sub(AR,sub(AS,sub(AT,sub(AU,sub(AV,sub(AW,sub(AX,sub(AY,sub(AZ,sub(BA,sub(BB,sub(BC,sub(BD,sub(BE,sub(BF,sub(BG,sub(BH,sub(BI,sub(BJ,sub(BK,sub(BL,sub(BM,sub(BN,sub(BO,sub(BP,sub(BQ,sub(BR,sub(BS,sub(BT,sub(BU,sub(BV,sub(BW,sub(BX,sub(BY,sub(BZ,sub(CA,sub(CB,sub(CC,sub(CD,sub(CE,sub(CF,sub(CG,sub(CH,sub(CI,sub(CJ,sub(CK,sub(CL,sub(CM,sub(CN,sub(CO,sub(CP,sub(CQ,sub(CR,sub(CS,sub(CT,sub(CU,sub(CV,sub(CW,sub(CX,sub(CY,sub(CZ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

...unless that is actual valid code?

ok i am starting to hate you dj.
WHY DIDN'T YOU TELL ME BEFORE I TRIED!!!!!!
lol i  am just kidding.  did you see the caps? really draws your attention doesn't it
Title: Re: Code optimization
Post by: DJ Omnimaga on June 18, 2010, 08:56:08 pm
Lol

Try to avoid abuse of them, though, because many french people visits this board and in our netiquette, all caps are considered as yelling, unlike on english boards, where they are considered as emphasis, so some people could take them as rude.

And nemo I was kidding with that code :P. It just basically meant to not go WAY too overboard either :P
Title: Re: Code optimization
Post by: nemo on June 18, 2010, 08:59:10 pm
psh, whether or not you were kidding i am going to try a program with that.  :D

edit: i'm twelve and what is this?
Title: Re: Code optimization
Post by: Happybobjr on June 18, 2010, 09:00:53 pm
sounds fun.  send me the code when you are done.

sry not trying to be rude XD
Title: Re: Code optimization
Post by: Magic Banana on June 18, 2010, 09:01:05 pm
Oh the possibilities ...
This makes things much more interesting.

@DJ Omni
What is this, I don't even ...
Title: Re: Code optimization
Post by: Happybobjr on June 18, 2010, 09:02:34 pm
the r1 and r2 code thing really helps me understand this update better
Title: Re: Code optimization
Post by: DJ Omnimaga on June 18, 2010, 09:02:56 pm
psh, whether or not you were kidding i am going to try a program with that.  :D
Just backup first, of course :P (I, myself, managed to forget that part, once, before testing a little program, even if I told myself I must backup first)
Title: Re: Code optimization
Post by: nemo on June 18, 2010, 09:11:27 pm
if axe supported floating point, i would make this the most epic quadratic solver known to man. because i'm lacking in that department, this will be.. interesting.
Title: Re: Code optimization
Post by: DJ Omnimaga on June 18, 2010, 09:17:23 pm
lol

Since this project started, I wished that floating point numbers support would only come after the first Axe program is uploaded on ticalc.org. I still have some bad memories of when I saw the very first ever BBC Basic language upload there. :P

Anyway back on topic, I assume the r1 through r6 stuff can be used inside sub() commands too, right? (such as sub(AB,r6,C)
Title: Re: Code optimization
Post by: calc84maniac on June 18, 2010, 10:21:58 pm
lol

Since this project started, I wished that floating point numbers support would only come after the first Axe program is uploaded on ticalc.org. I still have some bad memories of when I saw the very first ever BBC Basic language upload there. :P

Anyway back on topic, I assume the r1 through r6 stuff can be used inside sub() commands too, right? (such as sub(AB,r6,C)
Sure. Just be aware that they are stored in order, like:
r6->r1
C->r2

So if you do sub(AB,r2,r1)
it does
r2->r1
r1->r2
which is the same thing as doing sub(AB,r2,r2).

Basically, don't try to use r1 after the first argument, r2 after the second argument, etc.
Title: Re: Code optimization
Post by: nemo on June 18, 2010, 10:32:33 pm
i got a stack error when compiling it  :-\ oh well.
source is attached in case anyone wants to figure out what it would do (: note: i suggest you use sourcecoder or something of the like to view it.. since if you're viewing it on-calc you're going to have to scroll through about 40 lines of just sub()'s, and then 8 lines of just closing paranthesis. then you get to the alll the wonderful labels that make it possible.
Title: Re: Code optimization
Post by: Magic Banana on June 18, 2010, 11:06:29 pm
According to the Documentation:
ERR: STACK FULL There are too many parenthesis in a single expression.
Count your parentheses, you probably messed up somewhere.
Title: Re: Code optimization
Post by: nemo on June 18, 2010, 11:08:08 pm
there's a reason there are too many parantheses. that's because there are approximately 136 of them. at once. calling subroutines. trust me, i saw this comming.
Title: Re: Code optimization
Post by: Magic Banana on June 18, 2010, 11:14:16 pm
Oh wow. Just opened it up in wabbit and it's pretty crazy. I'll check it out in Sourcecoder.

Also, ;D Str1 ;D
Title: Re: Code optimization
Post by: DJ Omnimaga on June 18, 2010, 11:15:21 pm
lol

Since this project started, I wished that floating point numbers support would only come after the first Axe program is uploaded on ticalc.org. I still have some bad memories of when I saw the very first ever BBC Basic language upload there. :P

Anyway back on topic, I assume the r1 through r6 stuff can be used inside sub() commands too, right? (such as sub(AB,r6,C)
Sure. Just be aware that they are stored in order, like:
r6->r1
C->r2

So if you do sub(AB,r2,r1)
it does
r2->r1
r1->r2
which is the same thing as doing sub(AB,r2,r2).

Basically, don't try to use r1 after the first argument, r2 after the second argument, etc.
Aaah ok thanks for the info.

According to the Documentation:
ERR: STACK FULL There are too many parenthesis in a single expression.
Count your parentheses, you probably messed up somewhere.
Nah it's because there is a limit on the amount of nested parhenteses. I forgot how many, though.
Title: Re: Code optimization
Post by: nemo on June 18, 2010, 11:17:38 pm
hahahaha oh, Str1. what about it? it's just a bunch of mashed up words...   ::)
Title: Re: Code optimization
Post by: Quigibo on June 19, 2010, 01:40:55 am
LOL! Somebody actually got a stack error!  ;D  I didn't think that would ever happen.  I guess I will expand the maximum stack size a little bit.  Subroutines with arguments use 3 bytes of stack instead of 1 becasue they need to keep track of the name as well.  Some operations need 2 bytes in stack but most need 1.  The current maximum stack size is 64 bytes, If I have room, I will expand it to 128 bytes.  The operation stack is never searched through so that won't cause any slow downs during compiling.
Title: Re: Code optimization
Post by: nemo on June 19, 2010, 09:42:49 am
haha if you look at my code, there is a pretty definite reason why i got a stack error  :) i called 130+ (i think 136) subroutines in the same line. i'lll just take a quick copy paste from source coder:
Code: [Select]
:sub(V,sub(T,sub(U,sub(D,sub(V,sub(T,sub(U,sub(S,sub(V,sub(T,sub(U,sub(J,sub(V,sub(T,sub(U,sub(R,sub(V,sub(T,sub(U,sub(Q,sub(V,sub(T,sub(U,sub(P,sub(V,sub(T,sub(U,sub(B,sub(V,sub(T,sub(U,sub(A,sub(V,sub(T,sub(U,sub(O,sub(V,sub(T,sub(U,sub(N,sub(V,sub(T,sub(U,sub(B,sub(V,sub(T,sub(U,sub(A,sub(V,sub(T,sub(U,sub(M,sub(V,sub(T,sub(U,sub(D,sub(V,sub(T,sub(U,sub(L,sub(V,sub(T,sub(U,sub(B,sub(V,sub(T,sub(U,sub(A,sub(V,sub(T,sub(U,sub(D,sub(V,sub(T,sub(U,sub(K,sub(V,sub(T,sub(U,sub(J,sub(V,sub(T,sub(U,sub(I,sub(V,sub(T,sub(U,sub(H,sub(V,sub(T,sub(U,sub(B,sub(V,sub(T,sub(U,sub(A,sub(V,sub(T,sub(U,sub(G,sub(V,sub(T,sub(U,sub(D,sub(V,sub(T,sub(U,sub(F,sub(V,sub(T,sub(U,sub(B,sub(V,sub(T,sub(U,sub(A,sub(V,sub(T,sub(U,sub(E,sub(V,sub(T,sub(U,sub(D,sub(V,sub(T,sub(U,sub(C,sub(V,sub(T,sub(U,sub(B,sub(V,sub(T,sub(U,sub(Asub(V,sub(T,sub(U)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))


Title: Re: Code optimization
Post by: DJ Omnimaga on June 19, 2010, 09:44:46 am
LOL! Somebody actually got a stack error!  ;D  I didn't think that would ever happen.  I guess I will expand the maximum stack size a little bit.  Subroutines with arguments use 3 bytes of stack instead of 1 becasue they need to keep track of the name as well.  Some operations need 2 bytes in stack but most need 1.  The current maximum stack size is 64 bytes, If I have room, I will expand it to 128 bytes.  The operation stack is never searched through so that won't cause any slow downs during compiling.
oh wow as I started reading your post, I was about to reply to not worry too much because I doubt anyone would use this much of the stack, but then I continued, and I did not realize the stack was that smalll :O . Well I guess 128 bytes would probably be a good idea, providing you can find the space for it :)
Title: Re: Code optimization
Post by: calcdude84se on June 19, 2010, 03:19:46 pm
Nemo, wow. :P I don't think 128 bytes can contain that.
Edit: 300th post! Again! :P
Title: Re: Code optimization
Post by: nemo on June 19, 2010, 03:21:27 pm
probably not. if a sub( argument is 3 bytes.. and there are 130+ subs called as arguments to each other... you'd need about 400 bytes to contain it.
Title: Re: Code optimization
Post by: Builderboy on June 19, 2010, 03:34:30 pm
Do you think increasing the stack size to 128 is a good idea? What are the disadvantages?  Is it only memory?  Does it cost any speed?
Title: Re: Code optimization
Post by: calc84maniac on June 19, 2010, 04:30:01 pm
Do you think increasing the stack size to 128 is a good idea? What are the disadvantages?  Is it only memory?  Does it cost any speed?
I think this is the parser's stack he's talking about, it has no effect on the compiled program execution.
Title: Re: Code optimization
Post by: Builderboy on June 19, 2010, 04:55:43 pm
Ah i see, well in that case awesome :D
Title: Re: Code optimization
Post by: Quigibo on June 19, 2010, 05:03:46 pm
Assuming you mean the speed of compiling, no.  Its only memory which I can use for something else, like the block stack.  I increased it to 100 bytes.  That should be enough for anything sane.

In case anyone is curious, these are the current stats:

Operation Stack
Max size of nested expressions such as parenthesis
100 bytes = Anywhere from 30-90ish operations.

Block Stack
Max size of nested control blocks like "If" or "While"
96 bytes = 32 blocks

Symbol Stack
Max number of variable or label names like Str1A or Lbl ZZ
750 bytes = 150 of each
Title: Re: Code optimization
Post by: DJ Omnimaga on June 19, 2010, 05:07:51 pm
Mhmm, just so I understand the concept a bit more, does the stack use memory inside the compiled program (allocated during compiling) or does it uses something else in the calculator RAM? If so, then if your program doesn't use the entire stack anywhere, will it use as much memory? And would it count towards executable code or just data? Otherwise, is the stack the exact same location as with pure ASM programs?
Title: Re: Code optimization
Post by: Quigibo on June 19, 2010, 05:10:31 pm
I have enough free ram where I can rely totally on it and I don't have to allocate memory from the user ram.

EDIT: Just to clear confusion, this is just how the compiler works, it has nothing to do with the generated programs.
Title: Re: Code optimization
Post by: DJ Omnimaga on June 19, 2010, 05:25:53 pm
mhmm ok, I see. I guess we don't have to worry too much, then, except maybe to not go too overboard with nesting as I described a few posts (or pages?) ago