Re: Where is stringbuilder?
- From: erewhon@xxxxxxxxxx (J French)
- Date: Wed, 18 Oct 2006 07:35:04 GMT
On Wed, 18 Oct 2006 01:58:03 +0100, nowhere@xxxxxxxxxxxxxx wrote:
<snip>
That's exactly what Stringbuilder is.
When a string is added to another, such as s := s + t, instead of
simply tacking t on the end of s, a whole new block of memory is
allocated that can accommodate the new string. (the original s might
have something immediately after it, so concatenation in place might
not be possible).
The memory used by the original s is then abandoned, until the .NET
Garbage Collector reclaims it.
If you carry out a succession of string concatenations, you end up
leaving behind lots of little blocks of memory.
A Stringbuilder pre-allocates a larger block so that concatenations
can be done in-place. This has the effect of reducing the number of
memory allocations required, and the amount of work needed by the
Garbage Collector.
Win32 doesn't have a Garbage Collector, so Stringbuilders are
irrelevant.
I would guess that if you created a Win32 application in D2005/D2006,
you wouldn't have Stringbuilders either. (Or if you do, they're
probably just strings in reality).
Win32 or rather Delphi does not have a garbage collecter, but the
problem of 'Swiss Cheese' memory certainly exists.
If one /knows/ that one will be concatenation a whole load of strings,
or more precisely 'growing' just one string, then it makes sense to
create a longer string then push the smaller strings into their
destination positions.
This is what I use :-
{ ###########################################################
MidSet( L, 4, S )
}
Procedure MidSet( Var BigStr:String; Const HStart:Integer ; Const
SmallStr : String ) ;
Var
L9, BL, SL, L, Start : Integer ;
Begin
BL := Length( BigStr ) ;
SL := Length( SmallStr ) ;
Start := HStart - 1 ; { adjust start }
L := Min( SL, BL - Start ) ;
For L9 := 1 To L Do
BigStr[ L9 + Start ] := SmallStr[ L9 ] ;
End; {MidSet}
Alternatively one could use a Stream of some sort.
Personally I don't think that this is worth worrying about unless one
is doing a heck of a lot of concatenations on the same String
There is the old Concat Function, but it is not much use for this sort
of thing.
.
- Follow-Ups:
- Re: Where is stringbuilder?
- From: Maarten Wiltink
- Re: Where is stringbuilder?
- From: Rob Kennedy
- Re: Where is stringbuilder?
- References:
- Where is stringbuilder?
- From: brett
- Re: Where is stringbuilder?
- From: Rob Kennedy
- Where is stringbuilder?
- Prev by Date: Re: I suppose the idea of classes and inheritance is
- Next by Date: {$ELSEIF} not working in Delphi 2006 ?
- Previous by thread: Re: Where is stringbuilder?
- Next by thread: Re: Where is stringbuilder?
- Index(es):
Relevant Pages
|
Loading