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:

        A=Asc(Mid$(a$,3,1))
        Mid$(a$,6,1)=b$

Use this instead:

        A=Peek(Varptr(a$)+2)
        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:

        S$=Space$(1000)
        For X=1 To 1000
           S$=Left$(S$,Len(S$)-1)
        Next X

Use this instead:

        S$=Space$(1000)
        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:

        I=Instr(ST$,Chr$(0))

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.