Back on track now, here's a nerdy post for the holidays! :)
You hear about string performance a lot in .NET. People will sit there and add strings together in a loop causing thousands of allocations and garbage collections and gradually bring even the beefiest of servers to a halt. Well this isn't one of those posts. It's been proven countless times that you should use StringBuilder for that, and if you don't believe me you can try it for yourself.
This post is to answer one of my own personal pet peeves, which is how is it best to build a string from a bunch of other strings but not in a loop (for example combining UI elements and data from the application to make information appear in full sentences).
I got some very surprising results as well!
In order to make this test as realistic as possible, I performed 1 million concatenations of 6 strings using the 4 different methods:
String.Concat
string s = string.Concat("A", "B", "C", "D", "E", "F");
String.Format
string s = string.Format("{0}{1}{2}{3}{4}{5}", "A", "B", "C", "D", "E", "F");
StringBuilder
StringBuilder sb = new StringBuilder();
sb.Append("A");
sb.Append("B");
sb.Append("C");
sb.Append("D");
sb.Append("E");
sb.Append("F");
string s = sb.ToString();
Add Operator
string s = "A" + "B" + "C" + "D" + "E" + "F";
Now, we've all heard that adding strings together using the + operator is the biggest sin in the entire world... so observe these results:
Tests run:
Concat took 582 milliseconds
Format took 1505 milliseconds
Builder took 784 milliseconds
Add took 9 milliseconds
9 milliseconds? What? 50 times quicker than the next fastest time? Something can't be right there. So I fired up reflector to have a look at what was going on with the compiled assembly and found some very odd results. The code came out as the following (String.Format and StringBuilder were the same so have been omitted):
String.Concat
string s = "A" + "B" + "C" + "D" + "E" + "F";
Add Operator
string s = "ABCDEF";
Makes sense as to why the add function goes so quickly though, as the compiler optimises out the concatenation and just gives you the resulting string. The interesting part here though is what has happened to string.Concat. The compiler has replaced the call to string.Concat with the add operator, and because one optimisation has already occurred it doesn't then optimise this out into the raw string.
What is also interesting here are the results themselves. Concat was the quickest (closely followed by builder) which means that if you are sticking a bunch of strings together as a one off and not in a loop, String.Concat is the way forwards! Here's the controversial bit though, Concat translates into the + operator, which means for one shot string concatenation, + is actually the fastest way of doing it! Having said that, String.Format contains all sorts of Internationalisation magic and does clever things while formatting numbers and dates (when your concatenating things that aren't strings).
Until next time! :)
Currently sat on a National Express East Coast train enjoying their free Satellite Powered wireless internet while I journey home to see family for Christmas. Was planning to investigate properly into the Enterprise Library and all things shiny so that I can come back in January refreshed with newfound knowledge, but the prospect of IM on the go is way too tempting. It's Christmas! :)
Hope everyone out there has a good holiday.