In a follow-up to my previous post about string performance, I decided to do more of side-by-side comparison of the most common string concatenation techniques.
We all know these common techniques to be using the typical concatenation characters (& or +), String.Concat, and the StringBuilder. I have read many times and saw pretty graphs telling me about how high performing the String.Concat method is over the traditional concatenation method. I was highly surprised to find results to contradict these findings in my own testing.
So here is how I set-up this testing... I created a Console Application and created code to build out a list of a string object, and put that information into a dynamically generated HTML table. The code used to perform this is at the bottom of the blog entry. Within the HTML table code, I left it open to repeat the table row generation from the list object. This way, we can run as many iterations through string objects as we want.
The first test went as expected. I ran through a single iteration. The standard concatenation method was the only method to cost more than 0 milliseconds.
Bumping the iterations from 1 to 5 began to show me something I didn't expect. The String.Concat method and standard concatenation ran even with each other at 15 milliseconds, versus the StringBuilder which was still 0 milliseconds.
Doubling the iterations at 10 showed us that the String.Concat method was beginning to slow down in comparison to the standard concatenation method. The StringBuilder was still running under 0 milliseconds.
The previous finding remained true until we approach 500 iterations. Once there, we begin to see the StringBuilder actually spend some ticks getting finished. The other methods take longer, but the percentages are the same.
This puts a kink in the way I do things and what I have witnessed to work (outside of benchmarks). So I am not sure how I will proceed at this point.
Here is the code used to determine these findings:
Technorati Tags: Strings ,Performance ,VB