Re: Stringbuilder vs Strings

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On May 9, 6:02 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
It should be said that you should use StringBuilder to concatenate when
you don't exactly know how many strings you are going to concatenate. The
best example is when you have N number of strings and you need to append it
all together in a loop.

If you have a known number of strings, then using the concatenation
operator will work just fine, as it will compile to a call to the static
Concat method on the string class, which is very fast (it pre-allocates the
return buffer, and then does a memory copy of the strings into the
appropriate locations in the return buffer).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Andy" <a...@xxxxxxxxxxxxxxxxxx> wrote in message

news:1178718261.520466.20550@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

On May 9, 9:39 am, Dara P <D...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Can some one suggest me as why StringBuilder class is better than
Strings?

Its not better, it serves a different purpose. Use StringBuilder when
you have a lot of strings to concatinate, as the SB performs
concatination and other string manipulation operations faster than
using a standard string class.

Hello,

String class is immutable. Meaning any changes made to the reference
you have to a String object (such as concatenations), will be made on
a copy of the reference. Thus, as Nicholas mentioned, N strings
concatenations will allocate N strings for each concatenation.

StringBuilder uses double capacity allocation method, decreasing
allocation performance issues and assuring O(1) allocation complexity,
and it's better for multiple changes on a single string based objects
for the reasons mentioned above.

The following example will be much better using StringBuilder:

// This method will create almost 100000 String instances
String s = "MyName";
for( int i=0; i<100000; ++i)
s+=i.ToString();


// This will use only one instance and will allocate the space needed
for each append, efficiently. Much much better..
StringBuilder sb = new StringBuilder(s);
for( int i=0; i<100000; ++i)
sb.Append(i.ToString());

You can even make it better if you can predict the capacity prior to
making the changes. This will make sure allocation is made only when
necessary.

Hope it helps :)

.



Relevant Pages

  • Re: string concatenation optimizations [from python-dev Summary]
    ... Essentially what I have in mind is a type that's a list of byte ... The value is defined as the concatenation of these arrays. ... > way to concatenate a bunch of strings. ...
    (comp.lang.python)
  • Re: Resulting string from concatenation operator?
    ... that the operands must be either BIT or CHARACTER ... strings, and if not, the offending operand is converted. ... the result of the concatenation ... If the intermediate result of expression evaluation is a string it can be assigned to or passed to a variable or parameter of either fixed or varying length. ...
    (comp.lang.pl1)
  • Re: Resulting string from concatenation operator?
    ... that the operands must be either BIT or CHARACTER ... strings, and if not, the offending operand is converted. ... the result of the concatenation ... to the "other" attributes, in particular VARYING, ...
    (comp.lang.pl1)
  • Re: why use the data command parameter collection
    ... cumbersome than using an iterative object oriented approach. ... quote characters or other such input by concatenating strings. ... Parameters Collection. ... Also the approach string concatenation leaves a lot to be desired (see ...
    (microsoft.public.dotnet.framework.adonet)
  • Resulting string from concatenation operator?
    ... that the operands must be either BIT or CHARACTER ... strings, and if not, the offending operand is converted. ... the result of the concatenation ... to the "other" attributes, in particular VARYING, ...
    (comp.lang.pl1)