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:
00722043
Views:
45
Hi Stephan,

>Is there a way to use your approach whithout knowing the memory size to allocate ?

Yes. You would call the HeapReAlloc function when the allocated memory block is to small to hold the entire string. However, ReAlloc is a very slow operation as it copies memory around. If you would call it on every pass, you get the times of the pure VFP solution. Instead you could allocate bigger chunks of memory at once. Performance is directly affected by the size of the block you allocate at once. The following program demonstrate this when you modify the lnBlockSize parameter.
Declare Long GetProcessHeap in Win32API
Declare Long HeapAlloc in Win32API Long, Long, Long
Declare memcpy in NTDLL.Dll as memcpy_write Long, String@, Long
Declare Long HeapFree in Win32API Long, Long, Long
Declare Long HeapReAlloc in Win32API Long, Long, Long, Long

nStart = Seconds()
lnLoop = 1024*15
lnSize = 1024
lnBlockSize = 128*1024
lnAllocated = lnBlockSize
lnAddress = HeapAlloc( GetProcessHeap(), 0, lnAllocated )
lnPointer = lnAddress
For t=0 to lnLoop-1
	If lnPointer+lnSize > lnAddress+lnAllocated
		lnAllocated = lnAllocated+lnBlockSize
		lnAddress = HeapReAlloc( GetProcessHeap(), 0, lnAddress, lnAllocated )
		lnPointer = lnAddress + t*lnSize
	Endif 
	memcpy_write( lnPointer, Replicate("A",lnSize), lnSize )
	lnPointer = lnPointer + lnSize
Endfor 
? Seconds()-nStart
lcString = Sys(2600, lnAddress, lnLoop*lnSize )
HeapFree( GetProcessHeap(), 0, lnAddress )
? Seconds()-nStart
Christof
--
Christof
Previous
Reply
Map
View

Click here to load this message in the networking platform