AMOS:Optimizing Strings

From Amiga Coding
Jump to: navigation, search

Use Peek to read the ASCII value of a char in a string

speed increase: large

Use Peek(Varptr(a$)+x-1) instead of Asc(Mid$(a$,x,1)) and Poke Varptr(a$)+x-1,Asc(b$) instead of Mid$(a$,x,1)=b$.

Don't use:


Use this instead:

        Poke Varptr(a$)+5,Asc(b$) : Rem Asc(b$) could be predefined

Note: This is just under three times as fast!

Fast way to shorten a string

speed increase: medium

This routine is for making the string 1 character shorter. It may be helpful in an program to replace the input command (if you'd ever need to optimise such a routine which is unlikely).

Don't use:

        For X=1 To 1000
        Next X

Use this instead:

        POS=Varptr(S$) : Rem Put this statement inside the loop if you
                         Rem do any standard string operations.
        For X=1 To 1000
           Doke POS-2,Deek(POS)-1
        Next X

Note: This routine could be rather risky, as it is done outside the AMOS string handling system.

Use a string instead of Chr$()

speed increase: small

Don't use Chr$(code) in routines but replace them with a string.

Don't use:


Use this instead:

        C0$=Chr$(0)      : Rem At the beginning of your prog
        I=Instr(ST$,C0$) : Rem In the routine

Force garbage-collecting when you got spare time

speed increase: varies

A quasi-optimisation: Use "n=Free" at points where you don't care about the speed, to reduce the chances of the AMOS string garbage collector being called when you do care about the speed. This will only have an effect when strings are used in the routine.