C# · CodeProject · Dotnet

Better Approach to Clearing StringBuilder TIP

Dear Reader,

Lately i am refactoring many code and one such code i found out was that to append strings in a loop where its iteration count is pretty much unknown. Infact i have seen many code else where too where in many developers use stringbuilder to append strings before even knowing if it’s really required in that code chunk part. So in case if you have doubt when to choose what (string vs StringBuilder) then you must read Jon Skeets article.

So i saw this code chunk today in a foreach loop and i started to wonder why people write such code, let me show you the code i am talking about, which looks visibly flawless but carefully understood reveals the problem: (Yes the below code has constant string appending statements, but my actual code has variable numbers of strings to be appended)

for (int i = 0; i < 100000; i++)
{
StringBuilder sb = new StringBuilder();

sb.Append(“ada”);
sb.Append(“ada”);
sb.Append(“ada”);
sb.Append(“ada”);
sb.Append(“ada”);

var x = sb.ToString();
}

To look at the above code, its looks fine atleast for visibility sake. Now many people do this style because on every loop start they need a fresh SB instance so that they can append and do some operations on it. So many developers think to clear an SB items you need to create new instances in the loop.

But there is another very simple way, which you may already know about it. In case if you did not, just set the length property of SB to 0, that way the SB contents gets emptied and you can start appending again as if the SB is instantiated freshly.  Inface you also can call Sb.Clear() method too, but internally Clear() method sets Lenght property to 0.

The major benefits of doing this way is performance in creating object on heap. Since we all know that heap object creation is really a pain for CLR.  So i changed the above code a bit and it looks like this:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100000; i++)
{
sb.Length = 0;
sb.Append(“ada”);
sb.Append(“ada”);
sb.Append(“ada”);
sb.Append(“ada”);
sb.Append(“ada”);

var x = sb.ToString();
}

Now this way i am not creating new object on heap and on every loop start, i do get an empty SB so i can start filling it fresh.

To back up my words, i did a bit of performance analysis on the below code:

As per this code, i got these results as shown below:

TestMethod 1: Ticks = 472683843   Millisecond =157

TestMethod2 : Ticks = 941050287   Millisecond =314

So the output shows that resetting the length property value is far better than having new SB objects created every time.

Hope it helps, your comments are welcome.

Thanks 🙂

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s