Which row operation is better?

Possible duplicate:
Concatenation of strings against String Builder. Performance

Any difference (performance and memory usage) between the following two parameters:

option 1:

StringBuilder msgEntry = new StringBuilder(); msgEntry.AppendLine("<" + timeTag + ">" + timeStamp + "</" + timeTag + ">"); 

parameters 2:

 StringBuilder msgEntry = new StringBuilder(); msgEntry.Append("<"); msgEntry.Append(timeTag); msgEntry.Append(">"); msgEntry.Append(timeStamp); msgEntry.Append("</"); msgEntry.Append(timeTag ); msgEntry.Append(">\n"); 
+7
c #
source share
10 answers

The second is probably a little better in terms of memory usage, because it does not need to calculate intermediate line 1 ... but it is less readable, IMO.

Personally, I would use:

 msgEntry.AppendFormat("<{0}>{1}</{0}>", timeTag, timeStamp); 

After that, you did not show what you want to do with StringBuilder . If you are going to convert it to a string, I would use:

 string text = string.Format("<{0}>{1}</{0}>", timeTag, timeStamp); 

to start.

What is the performance? Well, probably worse - in the end, he had to parse the format string. But didn’t you measure it and find that it is a bottleneck, why are you worried?

Generally:

  • Make sure your architecture is efficient enough - it's hard to change later.
  • The balance of your interior design between efficiency and simplicity, with an emphasis on testability; changing the design later may take some time, but it should be practicable without compatibility issues.
  • Write your implementation as readable as possible.
  • Measure the system to find out if it works enough and where the bottlenecks are. They almost never will be in such code. (After all, we are not talking about string concatenation in a loop.)
  • When you find a bottleneck, try various optimizations and evaluate them. Do not assume that what you think will be faster will actually be faster.

1 Or an array to go to Concat ... we don’t know the type of timeStamp , so we can’t say exactly what happens there; in the second form, it can be added in place, while the first form may need to be inserted and then converted to a string before performing concatenation.

Exact implementation for redistribution, etc. may change between .NET 3.5 and .NET 4 (I know some implementation bits have). Without a very thorough benchmarking, I would be very sorry to say that it is faster ... but readability is easier to name, although subjectively.

+27
source share

All in all, StringBuilder ... but when you talk about performance, the only real test is measurement. Especially for LOTS string changes, StringBuilder is definitely the way to go. For a couple of lines ... maybe just add them using the + operator.

+3
source share

I would use .AppendFormat (); in this case

 StringBuilder msgEntry = new StringBuilder(); msgEntry.AppendFormat("<{0}>{1}</{0}>", timeTag , timeStamp); 
+2
source share

Personally, I would not choose a single one, and use

 msgEntry.AppendFormat("<{0}>{1}</{0}>", timeTag, timeStamp); 
+2
source share

If this is all you do, do not use StringBuilder. Too much overhead with a readability hit.

Try the following:

 string.Format("<{0}>{1}</{2}>", timeTag, timeStamp, timeTag); 
+1
source share

I relied on MSDN advice. Performance tips and tricks in .NET applications that recommend using StringBuilder for complex string manipulation.

The following is said:

Trade-offs There are some overheads associated with creating a StringBuilder, both in time and in memory. On a fast-memory machine, StringBuilder becomes useful if you perform about five operations. Typically, I would say 10 or more string operations are justification for the overhead of a machine, even slower.

I would also consider this tip from optimizing .Net show code:

This is especially important for pre-setting row size . If you don't, StringBuilder is still faster, but if you can predict the final length of the final string, set it in advance.

This is because the default capacity for StringBuilder is 16. It automatically changes when the capacity is exceeded - it doubles every time. Thus, you may have some unnecessary changes if you do not set the initial capacity. You can calculate the maximum expected number of characters in your example and initialize the StringBuilder so that it does not change. This will save some processor.

And here are some additional tips from MSDN :

The performance of concatenating an operation for a string or StringBuilder object depends on how often memory is allocated. The string concatenation operation always allocates memory, while the StringBuilder concatenation operation only allocates memory if the StringBuilder object's buffer is also small to accommodate new data. Therefore, the String class is preferred for concatenation if a fixed number of String objects are combined. In this case, individual operation concatenation can even be combined into one operation by the compiler. A StringBuilder object is preferred for concatenation if an arbitrary number of strings are concatenated; for example, if the loop combines a random number of user input lines.

+1
source share

The second line is better, since you do not get much, if you want, from the first line in the first. However, for a small concat string, such that I would not bother with stringbuilder

0
source share

Yes.

If you are going to make option 1, there is no point in using StringBuilder . You still concatenate strings and end up with a few extra temporary strings that are created and discarded in memory.

For something like this, you should probably use String.Format()

0
source share

The first example seems to me wrong. What is the point of creating a StringBuider object when concatenating strings before passing them to StringBuilder?

Also note:

  • Adds an instance of a StringBuilder instance, so calls to Append and AppendLine can be chained together
  • At the end of the second example, you can use AppendLine (">") instead of Append ("> \ n")
  StringBuilder msgEntry = new StringBuilder ();
     msgEntry.Append ("<")
         .Append (timeTag)
         .Append (">")
         .Append (timeStamp)
         .Append ("</")
         .Append (timeTag)
         .AppendLine (">");

Personally, I would do

  string.Format ("<{0}> {1} </ {0}>", timeTag, timeStamp); 

unless you need a StringBuilder for something else.

0
source share

Always use StringBuilder.Append() to concatenate strings. The line operator + invokes a new distribution for each new element.

-one
source share

All Articles