|
|
 |
 |
 |
 |
String builder
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
On Jun 5, 9:20 am, "simonZ" <simon.zu @studio-moderna.com> wrote:
> 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
-----------------------------------------------Reply-----------------------------------------------
simonZ wrote: > 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. -- Gran Andersson _____ http://www.guffa.com
-----------------------------------------------Reply-----------------------------------------------
"Gran Andersson" <g @guffa.com> wrote in message news:uiY9XQ1pHHA.3644@TK2MSFTNGP02.phx.gbl...
> simonZ wrote: >> 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. > -- > Gran Andersson > _____ > http://www.guffa.com
What about AppendFormat? -----------------------------------------------Reply-----------------------------------------------
On Jun 5, 12:21 pm, "Glenn" <glenn.csh @yahoo.co.uk> wrote: <snip> > 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
-----------------------------------------------Reply-----------------------------------------------
On Jun 5, 4:29 pm, "Jon Skeet [C# MVP]" <s @pobox.com> wrote:
> On Jun 5, 12:21 pm, "Glenn" <glenn.csh @yahoo.co.uk> wrote: > <snip> > > 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
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? -----------------------------------------------Reply-----------------------------------------------
On Jun 5, 1:17 pm, Aneesh Pulukkul <anees @gmail.com> wrote: > 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
-----------------------------------------------Reply-----------------------------------------------
> 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.
> 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. > -- > Gran Andersson > _____ > http://www.guffa.com
|
 |
 |
 |
 |
|