AMOS:Optimizing Math Operations

From Amiga Coding
Revision as of 02:20, 11 December 2008 by SamuraiCrow (talk | contribs) (Toggling A Value: Added example using Amos Xor command)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

use powers of 2 when multiplying or dividing

speed increase: large

Whenever possible use powers of 2 when multiplying or dividing. The AMOSPro Compiler will automatically optimise these to lsl and lsr. DON'T use the lsl or lsr commands in extensions, as they are over TWICE as slow!

Don't use:


Use this instead (if suitable):


As well as this, you can also do:

     n=x*160 -> n=x*(128+32) -> n=x*128+x*32 (tadaa)
     Also n=x*192 -> n=x*128+x*64 etc.

But this kind of thing is not faster:

     a=32 : b=7 : c=a*b

Prevent using floats

speed increase: large

Never use floating points. Use integers multiplied by a power of 2. You can actually decide what sort of precision you want in your decimal places and then multiply them out. For example, if you decide on about 2 point accuracy, you can multiply all your values by 128 (2^7, close enough to 100) and then do the calculations. When you have the results simply divide by 128 to get the required result.

Don't use:

        Plot x#,100

Use this instead:

        x=x*(3*128) : Rem Same as x=x*(1.5*256)
                      Rem 3*128 is calculated at compilation time
        Plot X/256,100


speed increase: large

Predefine as much as possible. Especially useful for Sin and Cos etc.

Don't use:

           If Sqr(x*x+y*y)=10 Then blah blah blah
        Until Something

Use this instead:

        ' This code should be placed at the beginning of your code.
        xmax=Maximum value of x : ymax=Maximum value of y
        Dim QUICKSQR(xmax,ymax)
        For x=0 to xmax
           For y=0 to ymax
           Next y
        Next x
        ' [Other code]
           If QUICKSQR(Abs(x),Abs(y))=10 Then blah blah blah
        Until Something

Don't use Inc, Dec or Add

speed increase: small

Use N=N+A instead of using Inc, Dec or Add. Although the manual claims they are faster, when using the AMOSPro Compiler, they actually turn out slightly slower.

Don't use:

        Inc a : Dec b : Add c,10

Use this instead:

        a=a+1 : b=b-1 : c=c+10

Note: Although Add is slower in it's short form, the full version with the base To top part is faster than the equivilant code.

This only applies to standard variables - DON'T use this with arrays!

NB: The Add command is also bugged when using "the power of" or ^ command (eg. If you define A as 2 and C as 2 and do Add A,C^2 you should find A=6. However it is bugged and gives some ridiculous result instead!)

Toggling A Value

If you want to toggle an option (for say music on/off) do not use:

        If KeyState(34) and _Music=0 Then _Music=1
        If Keystate(34) and _Music=1 Then _Music=0 

Instead use this far more efficient code:

        If Keystate(34) Then _Music=1-_Music

or use the undocumented Xor command:

        If Keystate(34) Then _Music=_Music Xor 1