From: | David McMinn |
Date: | 14 Mar 2001 at 17:55:22 |
Subject: | Re: How to optimize...? |
Hi Thorsten
> looks great, because of the "Goto Loop1" ;((( I dont have any ideas how to fix
> the "Goto Loop1" (Pop If, is missing!) and how to speed it up...
If you cannot avoid it, then using Goto to escape from an If...EndIf is OK. In
most cases, you should be able to re-write the code to use a different loop
structure to remove the Goto.
As for optimisation, the problem lies with the fact that you are storing the
string to print as a string, and working with strings. Consider the line:
a$=a$+NChr$(NPeekB(mem.l+adr.l))
What this line must do is this:
Add two longwords together [mem.l+adr.l]
Read a byte from memory [NPeekB(mem.l+adr.l)]
Convert to a character [NChr$(NPeekB(mem.l+adr.l))]
Allocate memory for a new string, which is 1 byte longer
Copy memory from old string to new string [a$=a$]
Copy character onto end of string [a$=a$+NChr$(NPeekB(mem.l+adr.l))]
Free memory for old string
This is probably the single area which makes your statement slow. It would be
much better if you were able to use a buffer of a fixed size. Also, using
b$=NLeft$(a$, i-1) to remove a single character is a waste, when you could have
easily done that in the loop.
Also, reading from memory lots (NPeekB) in one loop is useless if it is reading
the same piece of memory each time.
Setting the types of variables to be words speeds things up as well. When you
define y=0 then it uses the default type which is .q, and is quite slow (unless
you have a DEFTYPE.w somwhere in your program).
Where do you reset the a$ variable? Or is there some reason that you do not do
this? If you do not clear this string, you will print out what you have
previously printed before, as well as the current line.
I've changed your statement so that it uses a buffer instead of a string. I
hope this works, but you'll need to test it.
Statement MyShowText{adr.l} ; y and x are pos of TextPrint
y=0:x=0:txtc=255 ; txtc is the color of Font
; A fixed buffer which we can write the string into
; This uses space on the stack, so you do not want to make it too
; big. You could also make this a global variable and use it in
; this statement with SHARED
; This replaces the a$ and b$ in your loop
Dim buffer.b(1600)
bufpos.w = 0
; This is quite hacky, and is probably quite bad to do, but it means
; we do not need to poke and peek entire strings from memory. We would
; not have to do this if the FNSPrint command accepted pointers to
; memory as well as strings. I hope it works.
str$ = ""
str_backup1.l = NPeekL($str$)
str_backup2.l = NPeekL(&str$-4)
NPokeL &str$,&buffer(0)
; Why do you have +88 here?
For mem.l=0 To 50*30+88 ; PrintText is 50*30
If NPeekB(mem.l+adr.l)=$a Then y+8:mem.l+1:x=0
If NPeekB(mem.l+adr.l)=$25:mem.l+1:chg=1
If NPeekB(mem.l+adr.l)=$30 AND chg=1
txtf=0:chg=0:mem.l+1
buffer(0)=NPeekB(mem.l+adr.l):buffer(1)=0
bufpos=1:NPokeL &str$-4,bufpos
EndIf
If NPeekB(mem.l+adr.l)=$31 AND chg=1
txtf=1:chg=0
bufpos = 0
Repeat
mem.l+1
membyte = NPeekB(mem.l+adr.l)
buffer(bufpos) = membyte
bufpos + 1
Until membyte=$25
buffer(bufpos-1)=0
NPokeL &str$-4,bufpos
mem.l+1
EndIf
If chg=1
Select NPeekB(mem.l+adr.l)
Case $61: txtc=255
Case $62: txtc=254
Case $63: txtc=253
End Select
EndIf
EndIf
FNSPrint txtf,220+x,y,str$,0,txtc
If txtf=0 Then x+8
Next mem
; Restore the memory we previous hacked into the string
NPokeL &str$, str_backup1
NPokeL &str$-4, str_backup2
End Statement
> Is there any way to optimize my Statement and add some more of the FNS
> commands... Because i dont know all the commands ;(
Download the BSS from http://www.blitz-2000.co.uk (in the Archives/Software
page), you will get the full FNS package and docs from there. Alternatively,
the FNS package was given away on one of the BUMs I think, and the docs will be
in there. It was also given away with the free version of the RIBlitzLibs which
is on Aminet.
---------------------------------------------------------------------
To unsubscribe, e-mail: blitz-list/-unsubscribe@netsoc.ucd.ie
For additional commands, e-mail: blitz-list/-help@netsoc.ucd.ie