So, I'm primarily a C # and Java developer, but I suppose this question may be relevant to any programming languages ββthat use StringBuilder or some of its derivatives.
Well, so it is more or less known that concatenation of even a small number of lines can be the main cause of performance death (although this is debatable). My question is, does anyone know about the performance effects of using a string builder in a string builder. To clarify what I mean, let me include some kind of dummy code that should help illustrate my point.
In addition, I understand that there seems to be a high performance when calling multiple instances of StringBuilder, but I donβt think I would call it enough to cause any real performance issues. (This assumption may also be erroneous with respect to any opinion as to which would be useful 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());
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 should not be a problem, but something about this approach just doesn't seem right to me. Can anyone shed light on the following questions.
- This leads to a significant increase in performance than just using additional methods and using a single StringBuilder
- This is an acceptable practice.
Any place on this would be greatly appreciated.