Many .NET developers are baffled by the lack of a “Clear” method in the StringBuilder class. For example, if you are using a StringBuilder in a loop, you may want to clear its contents at the beginning of each loop.
Options to Clear a StringBuilder
It turns out there are two common ways to clear a StringBuilder:
Option #1: Create a new StringBuilder object
StringBuilder sb = new StringBuilder();
Option #2: Set its Length to zero
sb.Length = 0;
Which Option is Better?
Setting the Length property to zero is about 25% faster than creating a new StringBuilder object every time.
Option 2 seems intuitively better because it does not create a new object on the heap every time. Even so, it’s not likely to make a noticeable performance difference in your software. When looping a million times on a fast PC, setting the Length to zero is about 1 second faster than creating a new object.
Sample Console Program
Here is a simple console program that demonstrates this:
using System; using System.Text; namespace CSharp411 { class Program { static void Main( string[] args ) { int loops = 1000000; int maxLength = 100; DateTime time1 = DateTime.Now; for (int i = 0; i < loops; i++) { StringBuilder sb = new StringBuilder(); for (int j = 0; j < maxLength; j++) { sb.Append( 'a' ); } } DateTime time2 = DateTime.Now; StringBuilder sb2 = new StringBuilder(); for (int i = 0; i < loops; i++) { sb2.Length = 0; for (int j = 0; j < maxLength; j++) { sb2.Append( 'a' ); } } DateTime time3 = DateTime.Now; Console.WriteLine( "new = {0}, append = {1}", time2.Subtract( time1 ), time3.Subtract( time2 ) ); Console.ReadLine(); } } }
The results of this program on my PC were:
new = 00:00:04.1050000, append = 00:00:03.0690000
The difference is even smaller with faster PC’s. My results for 1M (your code):
new = 00:00:02.3437500, append = 00:00:01.9843750
So, while Option 2 is faster, nobody is really going to notice it anyway.
1. How would option 1 affect GC performance for 1M small object collection/compaction?
2. If we break through the ~85K large object size, would option 1 start fragmenting memory?
1. How would option 1 affect GC performance for 1M small object collection/compaction?
I assume it would slow down the GC, but perhaps not enough to worry.
2. If we break through the ~85K large object size, would option 1 start fragmenting memory?
By creating >85K strings? Then creating a new 85K StringBuilder object every time would definitely impact performance negatively.
According to these guys:
http://stackoverflow.com/questions/266923/is-using-stringbuilder-remove-method-more-memory-efficient-than-creating-a-new-st
Option 2 is faster if you initialize the capacity of the new StringBuilder (to 32 in their case).
If you want a Clear method for more intuitive syntax you can write an extension method on the StringBuilder class and set the length to 0 in that method.
Just for information of those who found this post via search engine, in .NET Framework 4.0 the StringBuilder class already have an “Clear” method.
So, just call: sb.Clear()
If you want to mesure time in asp.net I sugest use a StopWatch, is more acurate than un datetime.now…
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
{
do something
}
stopWatch.Stop();
Console.WriteLine(stopWatch.ElapsedMilliseconds.ToString());
[…] # By setting length equal to zero it make the same StringBuilder object re-usable without modifying the buffer Capacity. # The .NET Framework 4.0 provides the “Clear” Method to StringBuilder class which is equivalent to this implementation. For more deatil please follow the link below : http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.clear.aspx # For comparison of perfomance, please follow the below link: https://www.csharp411.com/clear-c-stringbuilder/ […]