I'm going to gloss over the details in how the worker threads got started or
how they communicate their results back to the UI. If anyone cares, I'll put
this all up on my web site plus the executable itself. Here's the
simplified worker thread itself. The same code is used for both string and
StringBuilder. The only difference is in the m_iID member. It can be a 0 or
a 1. If 0 then string concatenation takes place. Otherwise, StringBuilder is
used.
I tried a number of different ways to start with an empty StringBuilder.
Both removing the complete contents and creating a new StringBuilder each
time seemed to take about the same amount of time but, surprisingly, simply
setting the StringBuilder's Length to 0 to initialize the buffer contents to
empty ate up significantly more time.
private void WorkerThread()
{
// Let the world know we're running.
m_bRunning = true;
UpdateStatus( 0 );
try
{
string s;
StringBuilder sb = null;
// Generate an initial '1 second' limit.
int iEndTick = Environment.TickCount + 1000;
// This is the primary loop.
for (int iLoopCount=1; m_bRunning; iLoopCount++)
{
// If ID is zero then do the concatenation
// using string. Otherwise, use StringBuilder.
if ( (m_iID == 0))
{
s = "First";
s += "Second";
s += "Third";
s += "Fourth";
s += "Fifth";
s += "Sixth";
s += "Seventh";
s += "Eigthth";
s += "Nineth";
s += "Tenth";
}
else
{
sb = new StringBuilder();
sb.Append("First");
sb.Append("Second");
sb.Append("Third");
sb.Append("Fourth");
sb.Append("Fifth");
sb.Append("Sixth");
sb.Append("Seventh");
sb.Append("Eigthth");
sb.Append("Nineth");
sb.Append("Tenth");
s = sb.ToString();
}
// If the loop has been running for a second then
// update the UI status for this worker.
if ( iEndTick < Environment.TickCount )
{
UpdateStatus( iLoopCount * 10 );
iLoopCount = 0;
iEndTick = Environment.TickCount + 1000;
}
}
}
catch
{
}
finally
{
m_bRunning = false;
}
}
--
Richard Lewis Haggard
www.Haggard-And-Associates.com
"Bob Grommes" <bob@bobgrommes.com> wrote in message
news:%23qxnQrMZGHA.4916@TK2MSFTNGP04.phx.gbl...[color=blue]
> You'd have to share the code in order for anyone to make an accurate
> assessment.
>
> However, my experience is that there's a certain amount of overhead in
> newing up a StringBuilder and getting the string back out of it when
> you're done. So you have to be doing enough concatenation to overcome
> that. For example, if you use a StringBuilder to build an HTML page from
> a thousand bits, that will be much faster than repeated string
> concatenations. At the other extreme if you simply want to append
> something once then don't bother with StringBuilder. The crossover point
> will vary somewhat, but a good rule of thumb is that string concatenation
> is fine for 3 or fewer concatenations; consider a StringBuilder if you're
> doing more.
>
> As a final thought, I would not test the raw performance of a specific
> feature while mixing in the complication of threading issues. It's apt to
> muddy the waters too much.
>
> --Bob
>
> Richard Lewis Haggard wrote:[color=green]
>> I thought that the whole point of StringBuilder was that it was supposed
>> to be a faster way of building strings than string. However, I just put
>> together a simple little application to do a comparative analysis between
>> the two and, surprisingly, string seems to out perform StringBuilder by a
>> significant amount. A string concatenation takes not quite twice as long
>> using StringBuilder than it does with a string. This doesn't sound right
>> to me. Does anyone else have any hard performance data experience on the
>> subject?
>>
>> Here's what I did- I made a simple little app that starts two worker
>> threads, one uses string to concatenate string and the other uses
>> StringBuilder. Once a second, each of the worker threads reports the
>> number of concatenations it has performed to the UI which displays them.
>> The string thread generally runs at 3 million a second. The StringBuilder
>> turns in about 1.8 million. Does this run counter or agree with your
>> experience?[/color][/color]