>To all,
>I thought I'd pass along some interesting string manipulation behaviors I discovered trying to deal with arrays passed by reference from COM clients. I have 50k strings that need to go back and forth to COM clients. Under Japanese Windows, BSTR's go kaflooy(highly technical engineering term). We realized that safearray's are the way to go. I started filling the arrays with a simple loop but it takes a LONG time. After much flailing about, I noticed that looping and SUBST()'ing a table field was much faster than a memory variable. This is counter intuitive to me to say the least! Anyway, here is some quick code to illustrate what I found. Maybe you'll find it interesting.
>
>Gary Foster
>
>
>DIMENSION MyArray[50000]
>MyArray = chr(0)
>DO Compare witt MyArray
>
>
>PROCEDURE Compare
>LPARAMETER taPassedArray
>
>LOCAL cDataString
>LOCAL nStrLen
>LOCAL aa
>LOCAL bb
>LOCAL nInc
>
>EXTERNAL ARRAY taPassedArray
>
>*-- Fake up a string. I would normally get this from either a dbf or another COM server, same length in any case.
>cDataString = replicate('A', 50000)
>nStrLen = LEN(cDataString)
>
>*-- Fill array from cursor
>aa = seconds()
>CREATE CURSOR TempCursor (datastring M)
>INSERT INTO TempCursor (datastring) VALUES (cDataString)
>
>FOR nInc = 1 to nStrLen
> taPassedArray[nInc] = SUBST(TempCursor.datastring, nInc, 1)
>ENDFOR
>
>bb = seconds()
>MessageBox('SUBST(cursor.field) took '+allt(str(bb-aa,5,2))+' seconds')
>
>*-- Fill array from variable
>aa = seconds()
>FOR nInc = 1 to nStrLen
> taPassedArray[nInc] = SUBST(cDataString, nInc, 1)
>ENDFOR
>
>bb = seconds()
>MessageBox('SUBST(variable) took '+allt(str(bb-aa,5,2))+' seconds')
>
>ENDPROC
Gary,
Not much surprising. I tried to tell this before :) Memo files are kind of file streams. Also try this :
start = seconds()
FOR ix = 1 to 10
CREATE CURSOR TempCursor (datastring M)
INSERT INTO TempCursor (datastring) VALUES (cDataString)
FOR nInc = 1 to nStrLen
taPassedArray[nInc] = SUBST(TempCursor.datastring, nInc, 1)
ENDFOR
ENDFOR
MESSAGEBOX('SUBST(cursor.field) took '+allt(str(seconds()-start,8,4))+' seconds')
start = seconds()
FOR ix = 1 to 10
lcTempFile = "X"+sys(2015)+".Tmp"
handle = fcreate(lcTempFile)
=fwrite(handle,cDataString,nStrLen)
=fseek(handle,0,0)
FOR nInc = 1 to nStrLen
taPassedArray[nInc] = fread(handle,1)
ENDFOR
=fclose(handle)
ERASE (lcTempFile)
ENDFOR
MESSAGEBOX('Lowlevel took '+allt(str(seconds()-start,8,4))+' seconds')
A C routine (FLL) would be faster. ie: a 3Mb text file parsed distinct words, sorted and placed a cursor in fox takes under 3 secs. on a machine where above routines finish at about 5-6 secs.
Cetin