Note that there are some explanatory texts on larger screens.

plurals
  1. POUse of Multiple String Builders for one string
    primarykey
    data
    text
    <p>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. </p> <p>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. </p> <p>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) </p> <pre><code>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(); } </code></pre> <p>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 </p> <ol> <li>Does this create a <em>significantly</em> larger performance hit than just not using additional methods and using a single StringBuilder</li> <li>Is the a reasonably acceptable practice. </li> </ol> <p>Any onsite on this would be greatly appreciated. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload