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:
00722396
Views:
42
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.


>Christof,
>
>I'll reply in more depth later.. but clearly if I knew that I wanted a 15mb string allocation I'd do this:
>
>
lnStart = seconds()
>lcX = replicate( "A", 15 * 1024 * 1024 )
>? seconds() - lnStart
>
>which runs in 0.321 seconds on my machine. And puts it in the same time frame as the API calls you are making in the second code block. Instead of concatenating 15,360 1k long strings in a loop.
>
>
>>>VFP is as fast as the underlying C++ code when it is doing string concatenations of the form:
>>>
>>>lcX = lcX + ...
>>
>>It's certainly true that VFP is as fast as the underlying C code, however, this doesn't mean that it's fast code. VFP is actually quite slow when it comes to string handling compared to C++. The following program uses the standard approach to generate a 15 MB string:
>>
>>
>>nStart = Seconds()
>>lnLoop = 1024*15
>>lnSize = 1024
>>lcString = ""
>>For t=0 to lnLoop-1
>>	lcString = lcString + Replicate("A",lnSize)
>>Endfor
>>? Seconds()-nStart
>>
>>
>>On my computer this program runs for 409 seconds which is almost 7 minutes. On the other hand, the following program does exactly the same, but it finishes in 0.731 seconds. The actually string assembling is actually only 0.281. The rest of the time is what it takes VFP to copy the result into a string variable.
>>
>>
>>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
>>
>>nStart = Seconds()
>>lnLoop = 1024*15
>>lnSize = 1024
>>lnAddress = HeapAlloc( GetProcessHeap(), 0, lnLoop*lnSize )
>>For t=0 to lnLoop-1
>>	memcpy_write( lnAddress+(t*lnSize), Replicate("A",lnSize), lnSize )
>>Endfor
>>? Seconds()-nStart
>>lcString = Sys(2600, lnAddress, lnLoop*lnSize )
>>HeapFree( GetProcessHeap(), 0, lnAddress )
>>? Seconds()-nStart
>>
>>
>>Christof
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform