AMOS:Optimizing Traditional ways

From Amiga Coding
Jump to: navigation, search

Move invariant statement outside loops

speed increase: varies


Move invariant statement outside loops (Standard optimising compiler stuff).

If you have something like this:

        Repeat
           If x<a*20 Then do something groovy
        Until something

If the rest of the loop does not modify a, this can become:

        temp=a*20
        Repeat
           If x<temp Then do something groovy
        Until something


Unroll loops

speed increase: varies


Unroll your Loops!! Cuts down on Stack-Access eliminating Loop-overhead and replacing it with dedicated code. Don't go TOO overboard though. Normally five iterations is enough to gain a bit of speed. Too many iterations unrolled will actually lose speed on systems with CPU caches.

Don't use:

        For X=1 To 20
           UPDATE_ALIEN[X]
        Next X

Use this instead:

        For X=1 To 16 Step 5
           UPDATE_ALIEN[X]
           UPDATE_ALIEN[X+1]
           UPDATE_ALIEN[X+2]
           UPDATE_ALIEN[X+3]
           UPDATE_ALIEN[X+4]
        Next X

Note: You should only carry out this optimisation on small segments of code, such as plotting pixels, otherwise your code can get very bloated.


Eliminate stack access

speed increase: medium


Eliminate stack access (the reason Procedures are slower than Gosubs)

Don't use:

        For X=1 To 20
           Gosub MOVE_ALIEN : Rem This uses the stack 40 times
        Next X
        ' Other code
        MOVE_ALIEN:
        Moves Alien X
        Return

Use this instead:

        Gosub MOVE_ALIENS : Rem This only uses the stack twice.
        ' Other code
        MOVE_ALIENS:
        For X=1 To 20
           Move Alien X
        Next X
        Return

Also, try to move local variables out of procedures to make them global. They are significantly faster to use.