So I am primarily a C# and Java developer but I suppose this question could pertain to any programming languages that uses StringBuilder or some derivative thereof.
Okay so it is more or less common knowledge that concatenate even a small number of strings can be a major performance killer (although even that is debatable). My question is does anyone have knowledge of the performance effects of using a string builder within a string builder. To clarify what I mean by that let me include some dummy code that should help illustrate my point.
Also I realize that there is naturally a performance hit by calling multiple instances of StringBuilder but I do not believe that I would be calling it enough to cause any real performance issues. (This assumption may also be wrong any opinion on that would be helpful as well)
public string StringToGet()
{
StringBuilder sb = new StringBuilder("Some Text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append(MethodThatCreatesAnotherString());
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append(MethodThatCreatesAnotherString());
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append(MethodThatCreatesAnotherString());
//etc
return sb.toString();
}
private string MethodThatCreatesAnotherString()
{
StringBuilder sb = new StringBuilder("Other text");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
sb.append("more text to add");
return sb.ToString();
}
Logic tells me that this shouldn’t be a problem, but something about this approach just does not seem to look right to me. Can anyone shed some light on the following questions
- Does this create a significantly larger performance hit than just not using additional methods and using a single StringBuilder
- Is the a reasonably acceptable practice.
Any onsite on this would be greatly appreciated.
Typically, if the number of straight-up string concatenations are more than 5, it’s better to use
StringBuilder. If you have control over the methods that in turn build a string, why not consider passing yourStringBuilderinto the method?So, when it’s time to call that function, you pass in your current StringBuilder, and you don’t lose a performance hit, but it’s at the expense of potential obfuscation.
That said, unless you’re getting into the hundreds or thousands of these operations, I wouldn’t worry about premature optimization here. Your main performance hit is having to do it twice (i.e. append the strings inside the method, and then appending the complete string), but you still won’t be taking the performance hit to the extent of using
+concatenation.Edit: A clarifying example of how to use it:
So, you don’t ever append that string from the second method, you simply use the same
StringBuilderwithin that method. Hopefully that makes sense.