Benefits of using the StringBuilder Class

The old phrase, ‘Work Smarter, Not Harder’ is a good one to live by. As software developers, we often find ourselves writing code that build strings. We start out simple, with a few lines of code that concatenates different text together to create a new variable. It’s easy, quick and dirty.

string myString = “Counting to ten: “;
for (int i = 1; i < = 10; i++) myString += i + " ";

Later, this small string gets more complicated, becoming larger during each step of the software. Often we create clever loops and logic that dynamically builds long complicated strings. At the end of the day, it’s a ‘Bread and Butter’ activity for us.

You may be surprised to find out that this simple activity could be a major cause of a slow web application. Fortunately, a very simple fix can help you to gain back the performance and resources that you lost.

Why the Slow Performance?

Simply, inside .Net strings are immutable. If you haven’t seen this concept before, an immutable object is one that cannot be modified after it is created. This is beneficial since certain expensive operations can be avoided, allowing for a simpler implementation and faster execution.

You may be scratching your head. If a string cannot be modified, then how do I modify strings all day long?

Actually every time you modify a string, the .Net Framework creates a copy of the current string and modifies it. Effectively, every operation on the string creates a new string in memory. The old string is set aside for the garbage collector to cleanup and remove it from memory.

This isn’t bad if you create simple strings and do few operations on those strings. It becomes very expensive as you perform more operations on a string.

What Should I Do?

This is where the phrase, ‘Work Smarter, Not Harder’ comes into play. This StringBuilder Class was built to handle this issue and it does so with relative ease.

The first step is to create a new StringBuilder string, using the following code

System.Text.StringBuilder myString = new System.Text.StringBuilder(”Counting to ten: “);

Now you can add to the new string by using the StringBuilder’s Append method.

for (int i = 1; i < = 10; i++) myString2.Append(i + " ");

The effect of using StringBuilder Class is more pronounced as the strings get larger and the operations become more frequent. While there is a little more code involved, it is a minor adjustment to the previous code.

In Summary

The problem comes into effect whenever you have a lot of looping that is modifying a string. Each operation is expensive, since a new string is created every time. So in this case, you should use the StringBuilder class to create and modify the string.

Don’t be too quick to start to use StringBuilder for everything involving strings, though. If you are creating a basic string that will not be modified after it is first created, you should avoid using the StringBuilder class. The reason is that it will cost more in resources to instantiate the StringBuilder class, than it does to create a simple string.

So, in the end, it pays to know when to create simple strings and when to use the StringBuilder class. Happy coding …

Credits

1. Immutable Objects - http://en.wikipedia.org/wiki/Immutable_object

2. Using the String Builder - http://msdn2.microsoft.com/en-us/library/2839d5h5(VS.71).aspx

Leave a reply