Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I undermining the efficiency of StringBuilder?

I've started using StringBuilder in preference to straight concatenation, but it seems like it's missing a crucial method. So, I implemented it myself, as an extension:

public void Append(this StringBuilder stringBuilder, params string[] args) {     foreach (string arg in args)         stringBuilder.Append(arg); } 

This turns the following mess:

StringBuilder sb = new StringBuilder(); ... sb.Append(SettingNode); sb.Append(KeyAttribute); sb.Append(setting.Name); 

Into this:

sb.Append(SettingNode, KeyAttribute, setting.Name); 

I could use sb.AppendFormat("{0}{1}{2}",..., but this seems even less preferred, and still harder to read. Is my extension a good method, or does it somehow undermine the benefits of StringBuilder? I'm not trying to prematurely optimize anything, as my method is more about readability than speed, but I'd also like to know I'm not shooting myself in the foot.

like image 991
dlras2 Avatar asked Aug 13 '10 19:08

dlras2


People also ask

Is StringBuilder efficient in Java?

when concatenating strings, the StringBuilder is the most convenient option that comes to mind. However, with the small strings, the + operation has almost the same performance. Under the hood, the Java compiler may use the StringBuilder class to reduce the number of string objects.

How is StringBuilder more efficient?

Creating and initializing a new object is more expensive than appending a character to an buffer, so that is why string builder is faster, as a general rule, than string concatenation.

Should you use StringBuilder to construct large text instead of just adding?

I think we should go with StringBuilder append approach. Reason being : The String concatenate will create a new string object each time (As String is immutable object) , so it will create 3 objects. With String builder only one object will created[StringBuilder is mutable] and the further string gets appended to it.

Should you use StringBuilder to construct large text?

Answer. Use StringBuilders exclusively to concatenate many small strings into a large string, or even to concatenate a few large strings.


2 Answers

I see no problem with your extension. If it works for you it's all good.

I myself prefere:

sb.Append(SettingNode)   .Append(KeyAttribute)   .Append(setting.Name); 
like image 50
Jesper Palm Avatar answered Oct 09 '22 09:10

Jesper Palm


Questions like this can always be answered with a simple test case.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics;  namespace SBTest {     class Program     {         private const int ITERATIONS = 1000000;          private static void Main(string[] args)         {             Test1();             Test2();             Test3();         }          private static void Test1()         {             var sw = Stopwatch.StartNew();             var sb = new StringBuilder();              for (var i = 0; i < ITERATIONS; i++)             {                 sb.Append("TEST" + i.ToString("00000"),                           "TEST" + (i + 1).ToString("00000"),                           "TEST" + (i + 2).ToString("00000"));             }              sw.Stop();             Console.WriteLine("Testing Append() extension method...");             Console.WriteLine("--------------------------------------------");             Console.WriteLine("Test 1 iterations: {0:n0}", ITERATIONS);             Console.WriteLine("Test 1 milliseconds: {0:n0}", sw.ElapsedMilliseconds);             Console.WriteLine("Test 1 output length: {0:n0}", sb.Length);             Console.WriteLine("");         }          private static void Test2()         {             var sw = Stopwatch.StartNew();             var sb = new StringBuilder();              for (var i = 0; i < ITERATIONS; i++)             {                 sb.Append("TEST" + i.ToString("00000"));                 sb.Append("TEST" + (i+1).ToString("00000"));                 sb.Append("TEST" + (i+2).ToString("00000"));             }              sw.Stop();                 Console.WriteLine("Testing multiple calls to Append() built-in method...");             Console.WriteLine("--------------------------------------------");             Console.WriteLine("Test 2 iterations: {0:n0}", ITERATIONS);             Console.WriteLine("Test 2 milliseconds: {0:n0}", sw.ElapsedMilliseconds);             Console.WriteLine("Test 2 output length: {0:n0}", sb.Length);             Console.WriteLine("");         }          private static void Test3()         {             var sw = Stopwatch.StartNew();             var sb = new StringBuilder();              for (var i = 0; i < ITERATIONS; i++)             {                 sb.AppendFormat("{0}{1}{2}",                     "TEST" + i.ToString("00000"),                     "TEST" + (i + 1).ToString("00000"),                     "TEST" + (i + 2).ToString("00000"));             }              sw.Stop();             Console.WriteLine("Testing AppendFormat() built-in method...");             Console.WriteLine("--------------------------------------------");                         Console.WriteLine("Test 3 iterations: {0:n0}", ITERATIONS);             Console.WriteLine("Test 3 milliseconds: {0:n0}", sw.ElapsedMilliseconds);             Console.WriteLine("Test 3 output length: {0:n0}", sb.Length);             Console.WriteLine("");         }     }      public static class SBExtentions     {         public static void Append(this StringBuilder sb, params string[] args)         {             foreach (var arg in args)                 sb.Append(arg);         }     } } 

On my PC, the output is:

Testing Append() extension method... -------------------------------------------- Test 1 iterations: 1,000,000 Test 1 milliseconds: 1,080 Test 1 output length: 29,700,006  Testing multiple calls to Append() built-in method... -------------------------------------------- Test 2 iterations: 1,000,000 Test 2 milliseconds: 1,001 Test 2 output length: 29,700,006  Testing AppendFormat() built-in method... -------------------------------------------- Test 3 iterations: 1,000,000 Test 3 milliseconds: 1,124 Test 3 output length: 29,700,006 

So your extension method is only slightly slower than the Append() method and is slightly faster than the AppendFormat() method, but in all 3 cases, the difference is entirely too trivial to worry about. Thus, if your extension method enhances the readability of your code, use it!

like image 22
Chris Avatar answered Oct 09 '22 09:10

Chris