Ah. Did some digging around and you're right that
StringBuilder.ToString() doesn't allocate a new string - I'd
assumed that it would do. That would take your technique
of using StringBuilder down to 1 or 2 string allocations,
same as Greg's direct approach, meaning there's little
to choose between them.

And yeah I'd agree that if you're doing this with strings
from a database then a stored procedure is likely to be
better anyway.

Simon

---------------------------------------------------------------
Simon Robinson
http://www.SimonRobinson.com
---------------------------------------------------------------
----- Original Message -----
From: "Erick Thompson" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, May 03, 2002 2:28 AM
Subject: Re: [DOTNET] String concatenation performance


> Simon,
>
> I only see two string copies. When the StringBuilder is created, the
string
> is copied in (I believe, but I could see an internal optimization that
> wouldn't copy it). Then, the Insert methods and replace method all act on
> the StringBuilder, which should occur in-place. The final ToString()
method
> of the StringBuilder is a zero cost operation, as it just returns a
> reference to the char array contained in the StringBuilder as a string.
>
> But, I think you are right about Greg's method being more efficient, but
not
> by much.
>
> sb.Insert(2,sb.Replace("'", "''").ToString());
>
> I think that this method may cause the string returned by sb.Replace("'",
> "''").ToString() to be copied into the char array in the StringBuilder. If
> it does, then that makes two string copies, equal to Gregs method. But I
> think that the overhead of the additional calls to Insert probably make
the
> performance a little worse overall.
>
> But I think someone else said it best when they said that it shouldn't
make
> that much difference either way, and that a SqlParameter should be used
> instead :)
>
> Erick
>
>
> ----- Original Message -----
> From: "Simon Robinson" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Thursday, May 02, 2002 5:59 PM
> Subject: Re: [DOTNET] String concatenation performance
>
>
> > Erick
> >
> > Hate to say this but I'm afraid I don't agree with that. I think that
> > Greg had probably got the most efficient method.
> >
> > Greg uses a Replace followed by a string concatenation.
> > That means he'll have to copy the string either once
> > or twice: Twice if the
> > call to String.Replace() needs to do anything, or just one
> > allocation if the string contains no single quotes and the
> > call to Replace() has no effect.
> >
> > By contrast, using a StringBuilder,
> > you will always get at least two new string copy operations
> > - one to copy the original string into the StringBuilder object,
> > and another one to copy it back out again. Not only that,
> > but your call to String.Replace() will give a third string copy
> > if any single quotes do need to be replaced. Either way,
> > using a StringBuilder gives one more string copy than
> > Greg's method of manipulating the string.
> >
> >
> > Simon
> >
> > ---------------------------------------------------------------
> > Simon Robinson
> > http://www.SimonRobinson.com
> > ---------------------------------------------------------------
> > ----- Original Message -----
> > From: "Erick Thompson" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Friday, May 03, 2002 1:37 AM
> > Subject: Re: [DOTNET] String concatenation performance
> >
> >
> > > This should be faster. I'm not sure about the Insert method as far as
> > > performance goes but this method shouldn't cause any unneeded string
> > > allocations. However, I'm sure it could be improved on.
> > >
> > > public static string DoQuotes(string sqlParam) {
> > >     System.Text.StringBuilder sb = new
> > > System.Text.StringBuilder(sqlParam.Length + 2);
> > >     if (sqlParam.IndexOf("'") > 0) {
> > >         sb.Insert(2,sb.Replace("'", "''").ToString());
> > >     } else {
> > >         sb.Insert(2, sqlParam);
> > >     }
> > >     sb.Insert(1, "'");
> > >     sb.Append("'");
> > >     return sb.ToString();
> > > }
> > >
> > > Erick
> > >
> > > ----- Original Message -----
> > > From: "Greg Gates" <[EMAIL PROTECTED]>
> > > To: <[EMAIL PROTECTED]>
> > > Sent: Thursday, May 02, 2002 5:24 PM
> > > Subject: [DOTNET] String concatenation performance
> > >
> > >
> > > > Hello everyone:
> > > >
> > > > I have the following helper method to format sql string parameters:
> > > >
> > > > public static string DoQuotes(string sqlParam)
> > > > {
> > > >    if (sqlParam.IndexOf("'") > 0)
> > > >    {
> > > >       sqlParam = sqlParam.Replace("'","''");
> > > >    }
> > > >
> > > >    return "'" + sqlParam + "'";
> > > > }
> > > >
> > > >
> > > > Is there a better way, performance-wise?
> > > >
> > > > thanks, Greg
> > > >
> > > > 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