String builder

S

simonZ

I have array variable transfer: String[,] transfer which has some data
Than I would like to convert this data into string:

Which way is more efficient:

StringBuilder rezult=new StringBuilder();


for (int i = 0; i < transfer.Length; i++)
{
rezult.Append(String.Concat(transfer[0, 0].ToString(),",",transfer[0,
1].ToString(),",",transfer[0, 2].ToString()));
}

OR

for (int i = 0; i < transfer.Length; i++)
{
rezult.Append(transfer[0, 0].ToString()).Append(",").Append(transfer[0,
1].ToString()).Append(",").Append(transfer[0, 2].ToString());
}

How can I mesaure, which is more efficient?

Thanks,
Simon
 
J

Jon Skeet [C# MVP]

I have array variable transfer: String[,] transfer which has some data
Than I would like to convert this data into string:

Which way is more efficient:

StringBuilder rezult=new StringBuilder();

for (int i = 0; i < transfer.Length; i++)
{
rezult.Append(String.Concat(transfer[0, 0].ToString(),",",transfer[0,
1].ToString(),",",transfer[0, 2].ToString()));

}

OR

for (int i = 0; i < transfer.Length; i++)
{
rezult.Append(transfer[0, 0].ToString()).Append(",").Append(transfer[0,
1].ToString()).Append(",").Append(transfer[0, 2].ToString());
}

The latter is more efficient, but it's not very readable. Just call
Append several times in different statements. Note that your code is
completely ignoring the value of i at the moment, and you quite
possibly don't want to be using transfer.Length anyway (which is the
*total* size of the array, not the size of the first dimension).

Also, you don't need to call ToString() when you're using an array of
strings already.
How can I mesaure, which is more efficient?

Benchmark them - have a *big* array, and convert it to a string many
times, timing it. Are you convinced this is a bottleneck in your
application though? It's worth concentrating on readability over
performance until you've found a bottleneck.

Jon
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

simonZ said:
I have array variable transfer: String[,] transfer which has some data
Than I would like to convert this data into string:

Which way is more efficient:

StringBuilder rezult=new StringBuilder();


for (int i = 0; i < transfer.Length; i++)
{
rezult.Append(String.Concat(transfer[0, 0].ToString(),",",transfer[0,
1].ToString(),",",transfer[0, 2].ToString()));
}

OR

for (int i = 0; i < transfer.Length; i++)
{
rezult.Append(transfer[0, 0].ToString()).Append(",").Append(transfer[0,
1].ToString()).Append(",").Append(transfer[0, 2].ToString());
}

How can I mesaure, which is more efficient?

Thanks,
Simon

Neither of these is more efficient. ;)

This one is:

for (int i = 0; i < transfer.Length; i++) {
rezult.Append(transfer[0, 0]).Append(',').Append(transfer[0,
1]).Append(',').Append(transfer[0, 2]);
}

It's more efficient to add each string to the StringBuilder instead of
creating an intermediate string.

The Append method has overloads for different data types, so you don't
have to convert everything to strings first.

Appending a char value is more efficient than appending a single
character string.


I of course agree with Jon's comments about using the loop variable in
the loop, not using Length on a two dimensional array and not using
ToString on strings.
 
G

Glenn

Göran Andersson said:
simonZ said:
I have array variable transfer: String[,] transfer which has some data
Than I would like to convert this data into string:

Which way is more efficient:

StringBuilder rezult=new StringBuilder();


for (int i = 0; i < transfer.Length; i++)
{
rezult.Append(String.Concat(transfer[0, 0].ToString(),",",transfer[0,
1].ToString(),",",transfer[0, 2].ToString()));
}

OR

for (int i = 0; i < transfer.Length; i++)
{
rezult.Append(transfer[0, 0].ToString()).Append(",").Append(transfer[0,
1].ToString()).Append(",").Append(transfer[0, 2].ToString());
}

How can I mesaure, which is more efficient?

Thanks,
Simon

Neither of these is more efficient. ;)

This one is:

for (int i = 0; i < transfer.Length; i++) {
rezult.Append(transfer[0, 0]).Append(',').Append(transfer[0,
1]).Append(',').Append(transfer[0, 2]);
}

It's more efficient to add each string to the StringBuilder instead of
creating an intermediate string.

The Append method has overloads for different data types, so you don't
have to convert everything to strings first.

Appending a char value is more efficient than appending a single character
string.


I of course agree with Jon's comments about using the loop variable in the
loop, not using Length on a two dimensional array and not using ToString
on strings.


What about AppendFormat?
 
J

Jon Skeet [C# MVP]

What about AppendFormat?

If you just want to append a load of strings, I'd expect it to be
faster to call

builder.Append(first);
builder.Append(",");
builder.Append(second);
builder.Append(",");
builder.Append(third);

than

builder.Append("{0},{1},{2}", first, second, third);

because it doesn't need to process the format string.

On the other hand, the difference may well be negligible, and you may
consider the second form to be more readable, and that's much more
important in most situations.

Jon
 
A

Aneesh Pulukkul

If you just want to append a load of strings, I'd expect it to be
faster to call

builder.Append(first);
builder.Append(",");
builder.Append(second);
builder.Append(",");
builder.Append(third);

than

builder.Append("{0},{1},{2}", first, second, third);

because it doesn't need to process the format string.

On the other hand, the difference may well be negligible, and you may
consider the second form to be more readable, and that's much more
important in most situations.

Jon

using latter apporach - avoid String.Concat. I did't know the
performance hit for builder.Append("{0},{1},{2}", first, second,
third);. Thanks Jon. One doubt, if the no. of iterations is
comparively small say 5-10, in that case will it make much difference
if we use StringBuilder instead of String.Concat?
 
J

Jon Skeet [C# MVP]

using latter apporach - avoid String.Concat. I did't know the
performance hit for builder.Append("{0},{1},{2}", first, second,
third);. Thanks Jon. One doubt, if the no. of iterations is
comparively small say 5-10, in that case will it make much difference
if we use StringBuilder instead of String.Concat?

No, probably not. The difference becomes more pronounced the larger
the strings are and the more concatenations there are.

There's more detail here:
http://pobox.com/~skeet/csharp/stringbuilder.html

Jon
 
B

Ben Voigt [C++ MVP]

Neither of these is more efficient. ;)

This one is:

for (int i = 0; i < transfer.Length; i++) {
rezult.Append(transfer[0, 0]).Append(',').Append(transfer[0,
1]).Append(',').Append(transfer[0, 2]);
}

To get an even bigger win, loop through beforehand to determine the required
capacity, preallocate the StringBuffer, then append the strings.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top