AMOS:Optimizing Math Operations
Contents
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:
n=a*30
Use this instead (if suitable):
n=a*32
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:
x#=x#*1.5 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
Precalculate
speed increase: large
Predefine as much as possible. Especially useful for Sin and Cos etc.
Don't use:
Repeat 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 QUICKSQR(x,y)=SQR(x*x+y*y) Next y Next x ' [Other code] Repeat 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!