Fair enough, but take a look at the IL - there are no internalcalls for
set_Length so it's pretty transparent.  I've also done a rough test and
Length = 0 appears to be significantly faster than calling Remove, so even
if it does create a new buffer (which I really don't think it does), it's
still faster.

Looking at the Rotor source for StringBuilder (or 'StringBuffer' circa 1998)
also gives some clues as to how it might work under the hood.


Jim


> -----Original Message-----
> From: Jim Graf [mailto:[EMAIL PROTECTED]]
> Sent: 10 May 2002 15:57
> To: [EMAIL PROTECTED]
> Subject: Re: [DOTNET] StringBuilder Overhead
>
>
> Richter page 275:
>
>        // Clear the StringBuilder (allocates a new Char array).
>        sb.Length = 0;
>
> Huh?
> Jim
>
> >>> [EMAIL PROTECTED] 05/09/02 11:32AM >>>
> I'll probably get into trouble here, but if Richter said
> that, I think he
> was mistaken.  Setting Length = 0 is special-cased.  It sets the
> StringBuilder's internal string's length to zero (via a sneaky
> assembly-protected method on String), but a new character
> buffer is not
> created.
>
> I haven't tested it against Remove(), but I suspect it might
> be quicker.
>
> Jim
>
> > -----Original Message-----
> > From: Jim Graf [mailto:[EMAIL PROTECTED]]
> > Sent: 09 May 2002 15:51
> > To: [EMAIL PROTECTED]
> > Subject: Re: [DOTNET] StringBuilder Overhead
> >
> >
> > Thanks, Drew.
> >
> >   I read that but wanted to confirm that that was the case.
> >
> > Jim
> >
> > >>> [EMAIL PROTECTED] 05/09/02 09:31AM >>>
> > Jim Graf [mailto:[EMAIL PROTECTED]] wrote:
> >
> > >      Richter states that setting the length of a
> > > StringBuilder instance to 0 allocates a new character array.
> > >
> > > Does anyone know what the overhead of the following would be.
> > >
> > >          StringBuilder sb(600,2000)
> > >          ...use sb
> > >          sb.Remove(0,sb.Length);
> > >
> > > I want to reuse the same Stringbuilder over and over again.
> >
> > From the SDK docs on StringBuilder::Remove:
> >
> > <snip>
> > The current method removes the specified range of
> characters from the
> > current instance. The characters at (startIndex + length)
> are moved to
> > startIndex, and the string value of the current instance is
> > shortened by
> > length. The capacity is unaffected.
> > </snip>
> >
> > According to the last sentence, the character buffer will not
> > be affected.
> >
> > HTH,
> > Drew
> >
> > You can read messages from the DOTNET archive, unsubscribe
> > from DOTNET, or
> > subscribe to other DevelopMentor lists at
> http://discuss.develop.com.
> >
> > You can read messages from the DOTNET archive, unsubscribe
> > from DOTNET, or
> > subscribe to other DevelopMentor lists at
> http://discuss.develop.com.
> >
>
> You can read messages from the DOTNET archive, unsubscribe
> from DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.
>
> You can read messages from the DOTNET archive, unsubscribe
> from DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.
>

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to