Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Memory Pre-Allocation Trick?
Message
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
00721182
Message ID:
00722602
Views:
42
Here is a class i use to build large string, i stripped it a bit but you'll get the idea:
Local loString , lcString

nStart = Seconds()
loString = CreateObject("StringBuilder")

For i = 1 TO 1000
	loString.Add(Replicate("A", 1024))
EndFor

lcString = loString.Get()
? Len(lcString)

? Seconds()-nStart

Define Class StringBuilder As Line
	
	Protected nHandle, cFile
	
	Protected Function INIT()
	
		This.cFile = Addbs(Sys(2023))+Sys(2015)
		This.nHandle = Fcreate( This.cFile )
	
	EndFunc
	
	Function Add( cString )
		
		=Fwrite( This.nHandle, cString )
		
	EndFunc

	Function Get()
		
		Local lcRet
		
		If This.nHandle > 0
			=Fclose( This.nHandle )
			lcRet = FileToStr( This.cFile ) 
			Erase (This.cFile)
			This.nHandle = 0
		Else
			lcRet = ""
		Endif
		Return lcRet
		
	EndFunc
	
	Protected Function Destroy()
		
		If This.nHandle > 0
			=Fclose( This.nHandle )
			If File(This.cFile)
				Erase (This.cFile)
			Endif
			This.nHandle = 0
		EndIf
		
	EndFunc
	
EndDefine
Using this class, i never have to worry about the speed even with very large string.

>Stephane,
>
>In the context of the original message, it really doesn't matter what character fills the buffer.
>
>For all cases where I'm doing things like building HTML stings, simple string concatenations or the TEXTMERGE commands are fast enough. When you are adding strings it does make sense to optimize things. I know I improved the performance of the WWXML code by about 50% in some of the methods by hoisting invariants out of the loop and using some smaller temp strings rather than always concatenating every little piece to the final result. Something along the lines of:
>
>
lcResult = "<table>"
>n = afields( laFields )
>scan
>   lcRow = "<tr>"
>   for i = 1 to n
>      lcRow = lcRow + "<td>" + transform( eval( laFields[i,1] ) ) + "</td>"
>   endfor
>   lcResult = lcResult + lcRow + "</tr>"
>endscan
>
>lcResult = lcResult + "</table>"
>
>
>So this way the one big string only gets extended once per row of data rather than for every field. The code below shows a really slow way of doing the same thing:
>
>
lcResult = "<table>"
>scan
>   n = afields( laFields )
>   lcResult = lcResult + "<tr>"
>   for i = 1 to n
>      lcResult = lcResult + "<td>" +
>      lcResult = lcResult + transform( eval( laFields[i,1] ) )
>      lcResult = lcResult + "</td>"
>   endfor
>   lcResult = lcResult + "</tr>"
>endscan
>
>lcResult = lcResult + "</table>"
>
>
>
>If you are building lots of strings over several hundred K in size and absolute performance is an issue you might want to look at using a C++ DLL from VFP. There's a couple of examples on my website that show how C++ can really beat the pants off VFP for some string operations.
>
>
>>I don't use a 15 MB string of "A" very often in my programs :), the problem arise when you want to build a string with different values in a loop.
Previous
Reply
Map
View

Click here to load this message in the networking platform