pashov.net/code|Blog Archives

Tag Archives: stringbuilder

.NET

StringBuilder vs. Plus Operator

Published by:

One of the first things every developer learns is handling strings. String operations are so common that there is hardly a day without having to handle strings, so knowing the nitty-gritty of string handling is vital for every good developer.

It is something like a dogma to use StringBuilder when handling strings and not the + operator or String.Concat (which is + under the hood) because StringBuilder is way faster. Strings are immutable in C# so every time you want to change a string, you basically create a new string. StringBuilder solves this problem by keeping an array where it keeps the string chars and auto grows the array when needed (pretty much same principle as how Lists work in C#). The string object is then derived when calling the ToString method.

Although better performance is generally the case with StringBuilder VS the plus  operator, with string concatenation of only a few strings, i find the + operator  much more readable and the performance drain is close to zero so nothing to worry about. Consider the following example:


Stopwatch sbWatch = new Stopwatch();
Stopwatch plusOperatorWatch = new Stopwatch();
var destinationStringBuilder = new StringBuilder();
string destinationString = String.Empty;
string sourceString = new String('X', 20);

sbWatch.Start();
for (int i = 0; i <= 10000; i++)
{
    // Concat strings 3 times using StringBuilder
    destinationStringBuilder.Append(sourceString);
    destinationStringBuilder.Append(sourceString);
    destinationStringBuilder.Append(sourceString);
    destinationStringBuilder.Clear();
}
sbWatch.Stop();

plusOperatorWatch.Start();
for (int i = 0; i <= 10000; i++)
{
    // Concat strings 3 times using + operator concatenation
    destinationString = sourceString;
    destinationString += sourceString;
    destinationString += sourceString;
}
plusOperatorWatch.Stop();

On my machine the clock for StringBuilder shows 0 milliseconds and the clock for the plus operator shows 3 milliseconds which is very small difference for doing three string concatenations 10 000 times and in my opinion the + operator is much more readable.

The case, however, for StringBuilder is when we build larger texts out of smaller strings. If we remove

destinationStringBuilder.Clear();

from the first loop and convert

destinationString = sourceString;

to

destinationString += sourceString;

in the second loop

performance becomes an issue with StringBuilder outperforming + operator in the range of hundreds of times. StringBuilder takes 3 milliseconds and the + operator takes a little under 17 seconds.

So rule of thumb is if you have to do string manipulations with up to 5 strings go for the + operator for better readability, otherwise use StringBuilder for performance.

Consider the following example - a common practice is to write logs somewhere so i find much more readable to do something like :

var message="User " + user.userName + " tried to access resource " + resource.name + " at " + DateTime.Now;
Logger.info(message);

instead of:

var sb=new StringBuilder();
sb.Append("User ");
sb.Append(user.userName);
sb.Append(" tried to access resource ");
sb.Append(resource.name);
sb.Append(" at ");
sb.Append(DateTime.Now);
Logger.info(sb.ToString());

And performance difference in this case will be close to zero.