On Wed, 30 Jan 2008 05:59:52 -0800, Samuel R. Neff
<sa********@nomail.comwrote:
>There is no reason to ignore well-known performance tips just because
the net effect is small in many cases.
The use of StringBuilder decreases performance over naive concat in a
large number of cases I have seen it used.
Far too often, I've seen:
StringBuilder sb = new StringBuilder();
sb.Append("Foo ");
sb.Append(myVar.ToString());
sb.Append(" bar ");
sb.Append(someMethod());
etc(sb);
return sb.ToString();
Eugh! *Far* slower than a concat, and far less clear.
>It's always good to which
techniques perform better than others and do things the right way in
the first place.
In some languages, performance is often really important. Javascript is
a good example; it's an interpreted language and you're trying to do
interesting real-time stuff on somebody's 100MHz PC.
In Javascript, it's quite common to see loops like this:
for(var i = 0, l = myArray.length; i < l; i++)
{
doSomething();
}
It's faster - you don't have to get myArray.length more times than you
need to.
I strongly suspect this would also be faster in C# (the JIT compiler
might optimize it to the same code for arrays if it's good, but it
couldn't do Lists).
It's not a good idea - performance is rarely an issue in C#.
Generally, I think it's best to optimize for maintainability. The
majority of programs spend >95% of their CPU time in a few methods - you
only need to optimize for speed in those methods, if at all.
>I'm really amazed you're arguing against using StringBuilder or
teaching someone best practices.
Peter Duniho wrote:
I don't think he is. I think he's arguing that there are other concerns
than just performance. In many situations, using a string with
concatenation may be more readable or maintainable than using a
StringBuilder, and if those are situations in which it's known that the
concatenation will never be a noticable performance problem, then it
makes sense to use concatenation instead.
He's also quite right in pointing out that if you think you need to
pre-initialize the StringBuilder with some fixed constant, that "64" is
way too small to be useful (unless, of course, you have reason to
believe that your strings will _never_ exceed 64 characters).
Now, I think he may be overstating the relative likelihoods of the
comparative scenarios. Some applications may spend a lot of time
building new strings from numerous small pieces. While the majority of
applications may not have this characteristic, that doesn't mean that
there aren't a good number of correct implementations that would require
that behavior.
StringBuilders get abused both as a 'fast' concat, and a pseudo-stream.
Simple programs that manipulate strings should generally work on
streams. More complex programs should generally parse the string into a
more sensible internal structure and work on that instead.
Nice UNICODE strings are lovely for humans, but about the worst possible
choice of internal data type.
It doesn't help that people who havn't quite figured this out yet get
told "it's okay - just use a StringBuilder instead"
So if anything, I would disagree with the statement that
"things are very wrong" if an application needs to use StringBuilder.
Sometimes, you have to maintain code that should have got the author
many years in jail. Perhaps it's doing some complicated mathematics by
constructing an huge SQL query to send to a database server and getting
the result back as a scalar. (Yes, people really do write code like this)
If it needs to be more performant, you might profile it and find that
it's bound by the speed it can concat strings together in a particular
method. If it doesn't make economic sense to do a total rewrite, a quick
and dirty solution would be to change that method to use a StringBuilder.
There are few good reasons to use a StringBuilder outside of this
scenario (although I will admit they do exist - as do good reasons to
use TypedReferences, try{}IL-fault{} blocks, VB.NET, etc).
Alun Harford