On Mon, 03 Nov 2008, Randy Portnoff wrote:

Hi Randy,

> If you have a string that will expand in size by concatenating to it, for 
> example...
> local cText := ""
> cText += "Some text"
> cText += "More text"
> ...
> cText += "Even more text"

Harbour detects such situation and preallocate buffer for next += operations
to increase the speed.

> Is there any benefit in allocating the final size of the string first - For 
> example...
> local cText := Space( 2000 )
> cText := ""
> cText += "Some text"
> ...

No though it may interact with OS behavior when you allocate big peace of
memory. But the results does not have to be repeatable.

> Also, when reusing a string, does first setting it to NIL have any benefits 
> to memory - For example...
> local cText
> cText := "Some text"
> ...
> cText := nil  // Does this help memory usage at all?
> cText := "New text"

No. The above only introducing few additional PCODEs so the code is slower
because "New text" does not allocate new memory. It's static string.
Anyhow if you are asking when memory is freed if cText is dynamic string
in cText := <neVal> operation then it's done after creating <newVal>
so cText := NIL frees it before and allows to reuse this memory. Anyhow
it does not have to give faster execution or reduce total memory consumption
because MM keeps some pool of memory ready for reuse immediately without and
low level OS calls. Such manyally optimizations usually gives slower and
bigger code because new PCODEs are added and badly interacts with memory
usage statistics dynamically created by memory manager use to reduce low
level OS calls for memory request/release.
In some situation when cText is very big string item allocating many
megabytes it may help if you release this item as soon as possible, f.e.
by assigning NIL to it so the memory will be returned to OS and can be
reused by other programs which may also need it but that's all.

best regards,
Przemek
_______________________________________________
Harbour mailing list
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to