Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Allocating memory to variable
Message
From
27/11/2009 10:05:55
James Blackburn
Qualty Design Systems, Inc.
Kuna, Idaho, United States
 
 
To
26/11/2009 23:52:44
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MySQL
Miscellaneous
Thread ID:
01436706
Message ID:
01436750
Views:
66
This message has been marked as a message which has helped to the initial question of the thread.
Hi Christian,

I got the following from an artical in a magazine (can't remember the name at the moment) written by Christof Wollenhaupt. This will preallocate memory before building the string.
aa=CREATEOBJECT("stringbuilder")
scan
  aa.append(yourstring)
endscan 

return aa.getstring()

DEFINE CLASS stringbuilder AS acusbase OF aappcontrols.prg
	#if .f.
		local this as stringbuilder of  stringbuilder.prg
	#endif 

	nBlockSize = 64*1024
	nNextChar = 0
	nAllowcated = 0
	nPointer = 0
	
	FUNCTION init(tnInitSize)
		LOCAL llRet 
		llRet = .t.
		DECLARE Long GetProcessHeap IN Win32API 
		DECLARE Long HeapAlloc IN Win32API Long, Long, Long
		IF VARTYPE(m.tnInitSize) == "N"
			this.nallowcated = tnInitSize
		ELSE 
			this.nallowcated = this.nblocksize
		ENDIF 
		this.npointer = HeapAlloc(GetProcessHeap(),0,this.nallowcated)
		this.nNextchar = this.npointer 
		IF this.npointer = 0
			ERROR "out of memory"
		ENDIF 
		return llRet 
	ENDFUNC
	
	FUNCTION destroy()
		LOCAL llRet 
		llRet = .t.
		DECLARE Long GetProcessHeap IN Win32API 
		DECLARE Long HeapFree IN Win32API Long, Long, Long
		IF this.npointer <> 0
			HeapFree(GetProcessHeap(),0,this.npointer)
		ENDIF 
		return llRet 
	ENDFUNC

	FUNCTION append(tcString)
		LOCAL llRet , lnLength, lnSize
		llRet = .t.
		DECLARE Long GetProcessHeap IN Win32API 
		DECLARE Long HeapReAlloc IN Win32API Long, Long, Long, Long
		lnLength = LEN(m.tcString)
		IF this.nNextchar+m.lnLength > this.npointer+this.nallowcated 
			lnSize = this.nNextchar - this.npointer 
			this.nallowcated = this.nallowcated + this.nBlockSize 
			this.nPointer = HeapReAlloc(GetProcessHeap(),0,this.npointer,this.nallowcated)
			IF this.npointer = 0
				ERROR "Out of memory"
			ENDIF 
			this.nnextchar = this.npointer + m.lnSize
		ENDIF 
		SYS(2600,this.nnextchar,m.lnLength,m.tcString)
		this.nnextchar = this.nnextchar + m.lnLength
		return llRet 
	ENDFUNC
	
	FUNCTION getstring()
		LOCAL lcRet 
		lcRet = SYS(2600,this.npointer,this.nnextchar-this.npointer)
		return lcRet 
	ENDFUNC

ENDDEFINE
>Hello,
>
>I need to do the following:
>1) Query 100,000 lines from different tables and extract a string of about 200 characters per line,
>2) Add each line to a variable plus line break,
>3) Create a file (using StrToFile()) from the variable.
>
>I wonder how I can prevent performance problems by calling 100,000 times lcString = lcString + lcLine. This is going to re-allocate memory all the time, and I like to pre-allocate the needed memory for best performance. I remember having read an article about this many years ago (from Christopher) but I really do not remember anything about it, so I would be very thankful if somebody would give me a tip.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform