AMOS:Optimizing Comparisons

From Amiga Coding
Jump to: navigation, search

Don't use Sgn(n), use n<0 and n>0 instead

speed increase: small


Don't use:

        If Sgn(n)=-1 Then blah blah
        If Sgn(n)=1 Then blah de blah

Use this instead:

        If n<0 Then blah blah
        If n>0 Then blah de blah


Use Abs(n)<v instead of n>-v and n<v

speed increase: medium


Don't use:

        If n>-5 and n<5 Then blah

Use this instead:

        If Abs(n)<5 Then blah


Use < and > instead of <= and >= whenever possible

speed increase: small


They are slightly faster. Strange, but true.


When comparing immediate-values Don't use:

        If n<=10 Then blah

Use this instead:

        If n<11 Then blah


But, when using variables, don't use this kind of thing:

     A=10
     If B>A-1 Then etc

instead of:

     A=10
     If B>=A Then etc


Don't do stuff like If n=True, If n=False, or If n<>0

speed increase: medium


Don't use:

        If a=True or b=False or c<>0 Then blah blah

Use this instead:

        If a or Not(b) or c Then blah blah

But ONLY do this if you are sure b is a boolean value. "Not" simply bitflips an integer so Not -1 is 0, but Not 2 is $FFFFFFFE.

AMOSPro is rather buggy with the Not() command, so you may have to experiment a bit.

Also, using =False instead of =0 is faster. Another trick you should be aware of is that you can use the optimised version of <>0 where you would normally use >0 (or <0) if the routine only returns values >=0.


Don't use:

        If Instr(a$,"Something")>0 Then blah

Do this instead:

        If Instr(a$,"Something") Then blah