Given most programming problems there is an almost endless number of ways to implement a solution for even the most simple task. One of those tasks that many take for granted is string manipulation. When talking to a friend recently about this it brought up the question of which method is faster and better performing. So I decided I needed to look at execution speeds and memory usage of three of the main string manipulation implementations; String.Format, StringBuilder and String Concatenation.

To do this this testing I developed several test cases within the same project and monitored execution speed and memory usage at the end of each case. All tests were done in C# .NET 4.0. My end result is a sample e-mail body that contains several dynamic elements.

I created the following three code snippets in order to test my theories.

Test Case 1: String Concatenation

string s = "Date: " + DateTime.Now.ToString();
s += Environment.NewLine + Environment.NewLine;
s += "Hello " + sRecipientName;
s += Environment.NewLine + Environment.NewLine;
s += "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce consequat fringilla nulla at ultrices. Ut odio dui, commodo id porta a, ultricies quis nulla. Aliquam sollicitudin lectus quis nisl varius fringilla. Morbi tempor nunc pulvinar tellus ultrices sodales. Integer libero nunc, fermentum in tempus et, laoreet ut mi. ";
s += Environment.NewLine + Environment.NewLine;
s += "Nullam mollis nunc eu lorem mattis ac facilisis ligula suscipit. Integer imperdiet dolor adipiscing eros semper at rutrum lectus facilisis. Nam a urna justo. Aenean magna leo, rutrum dapibus facilisis quis, interdum nec lacus. ";
s += Environment.NewLine + Environment.NewLine;
s += "Sincerly, ";
s += Environment.NewLine + Environment.NewLine;
s += sSenderName;

Console.WriteLine(s);

Test Case 2: String Builder

StringBuilder sb = new StringBuilder();

sb.Append("Date: ");
sb.Append(DateTime.Now);
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
sb.Append("Hello ");
sb.Append(sRecipientName);
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
sb.AppendLine("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce consequat fringilla nulla at ultrices. Ut odio dui, commodo id porta a, ultricies quis nulla. Aliquam sollicitudin lectus quis nisl varius fringilla. Morbi tempor nunc pulvinar tellus ultrices sodales. Integer libero nunc, fermentum in tempus et, laoreet ut mi. ");
sb.Append(Environment.NewLine);
sb.AppendLine("Nullam mollis nunc eu lorem mattis ac facilisis ligula suscipit. Integer imperdiet dolor adipiscing eros semper at rutrum lectus facilisis. Nam a urna justo. Aenean magna leo, rutrum dapibus facilisis quis, interdum nec lacus. ");
sb.Append(Environment.NewLine);
sb.AppendLine("Sincerly, ");
sb.Append(Environment.NewLine);
sb.Append(sSenderName);

Console.WriteLine(sb);

Test Case 3: String.Format

Console.WriteLine(String.Format("Date: {0}{1}{2}Hello {3},{4}{5}Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce consequat fringilla nulla at ultrices. Ut odio dui, commodo id porta a, ultricies quis nulla. Aliquam sollicitudin lectus quis nisl varius fringilla. Morbi tempor nunc pulvinar tellus ultrices sodales. Integer libero nunc, fermentum in tempus et, laoreet ut mi. {6}{7}Nullam mollis nunc eu lorem mattis ac facilisis ligula suscipit. Integer imperdiet dolor adipiscing eros semper at rutrum lectus facilisis. Nam a urna justo. Aenean magna leo, rutrum dapibus facilisis quis, interdum nec lacus. {8}{9}Sincerly, {10}{11}{12}", DateTime.Now, Environment.NewLine, Environment.NewLine, sRecipientName, Environment.NewLine, Environment.NewLine, Environment.NewLine, Environment.NewLine, Environment.NewLine, Environment.NewLine, Environment.NewLine, Environment.NewLine, sSenderName));

For my initial pass through I did 3 runs with just creating the string once and outputting it to the console recording total memory usage and execution time. My results are as follows. Speed results are in seconds and memory results are in bytes.

First Pass on String Tests

As you can see from the above my results were a little under satisfying as they really didn’t answer my question one way or another. So I decided to take another run through of the three test cases, this time executing each one 100 times to give me a bigger picture on which one is faster. Here are my results:

Second Pass on String Tests

This second pass through offers a little bit more insight into the differences between the runs.

The tldr;

Straight string concatenation in this test was the fastest but also the biggest memory hog. The String Builder was the slowest but used up the least amount of memory on average. String.Format came in the middle of the two but also has the most difficult to understand code. So in the end it’s really comes down to what you are looking for. If you want the fastest performing go with string concatenation. For the lowest overhead go with a String Builder. Then if you want job security (because no one else can understand your code) go with String.Format for stats down the middle.

You can download the source for this test project here.

Share

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
© 2010 Michael Merrell Part of the AFK ATM Network Suffusion theme by Sayontan Sinha