Click here to Skip to main content
15,885,767 members
Articles / General Programming / Performance
Tip/Trick

Performance analysis for String and StringBuilder

Rate me:
Please Sign up or sign in to vote.
4.07/5 (7 votes)
23 May 2012CPOL4 min read 26.9K   1   15
Performance analysis for String and StringBuilder

Performance analysis for String and StringBuilder

Sometimes small-small changes in our code really makes a huge difference to a performance. There are many tips and tricks available and among those, one I am going to discuss over here. I'll be talking about String vs StringBuilder. One needs to be very careful while playing with strings because memory wise there is a huge impact of strings. I know, there are lots and lots of articles available on net on String and StringBuilder, but still I am going to show this, using some statistics.

Here I am taking Fx 4.0 C# console application with different static methods to showcase my analysis. Basically what I am doing here is, I am having a String variable named outputString and just looping that for 1000 times and concating the string to variable outputString.

Image 1

Please note, concatenation is done using + symbol. So, what happens internally is, whenever concatenation is done using + symbol, every time, new String object is created. So, as with my snippet. Here I am looping 1000 times, so, it is creating 1000 String objects and every time is is replaced with variable outputString . Thatway, whenver we use string concatenation with the plus (+) sign, it is definately going to cost our application performance.

Well, I guess this much boring theory is enough. Let's move towards statistics.

Here I am using CLR Profiler and is really one of the good tool to analyse our code performance. This tool tells us, how much memory bytes are consumed, Garbage Collector performance and how many objects it is moving to generation Gen0, Gen1 and Gen2 buckets. And at the same time statistics provided by this tool is very easy to understand.

Ok, I just ran CLR Profiler for the above code and got the below statistics. Here I am not going to cover GC generations in detail, but would like to touch bit on it. One must know that all the objects created in application, first comes to G0 bucket and then older objects are moved to G1 bucket. If the G1 bucket is going to full then older objects get moved to G2 bucket. But for .Net GC, frequency of visiting G1 and G2 is very less, compare to the G0 bucket. It means that GC is visiting bucket 0 frequently, so it is releasing G0 objects much frequently and the scope of object is also very less. So, if your application is creating objects which lot many objects are moving to G1 and G2, then it is not a good sign.

Now quickly jumping back to our example:

Image 2

Here we see that heap bytes are present in all three Gen 0,Gen 1,Gen 2 and even the memory wise also it is 7 digit (2,894,353).

Here Relocated bytes means it is going to be the part of G1 related objects. Here I am not going to analyse all the result, but somehow we are seeing here some negative signs because few of the objects are falling in G1 and G2 buckets also.

Now before commenting on it, lets take StringBuilder's data. In this example, I just created a StringBuilder instance named sb. Here I am doing the same thing, but instaed of string, I am taking instance of StringBuilder. In case of StringBuilder, whenever value will be appended, it will not create any new object but just updates the reference of the sb object with the new value. So, internally it is not creating a new object for every concatination. So, this is the real benefit of StringBuilder as compare to Stirng object.

Image 3

Although we are looping for 1000 times, but it doesn't mean that we are creating 1000 string objects. That's the way we are controlling memory usage and creation of new objects. Now will run profiler and checkout the results.

Image 4

Here we see that memory bytes are reduced to 5 digits (92, 332) and relocated bytes are nothing. If we will see that Heap bytes, it is unknown (0) for all G0, G1 and G2. It means, none of the objects are moved to G1 and G2. All the objects are created in G0 and release from G0 itself.

So, here we noticed that there is a significant difference in both memory usage as well as GC's bucket movements.

So, here we can conclude that we should prefer to use StringBuilder, rather than String specially when we are dealing with concatenations.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 5 Pin
Lokesh Zende29-Jul-12 23:52
professionalLokesh Zende29-Jul-12 23:52 
GeneralMy vote of 4 Pin
pratapvarun2-Jun-12 6:40
pratapvarun2-Jun-12 6:40 
GeneralMy vote of 4 Pin
honeysinghi23-May-12 17:17
honeysinghi23-May-12 17:17 
QuestionIts nice just that the location is wrong. Pin
Shivprasad koirala23-May-12 4:27
Shivprasad koirala23-May-12 4:27 
AnswerRe: Its nice just that the location is wrong. Pin
Shweta Lodha23-May-12 4:30
Shweta Lodha23-May-12 4:30 
GeneralThoughts Pin
PIEBALDconsult21-May-12 7:39
mvePIEBALDconsult21-May-12 7:39 
GeneralMy vote of 1 Pin
Andreas Gieriet20-May-12 21:23
professionalAndreas Gieriet20-May-12 21:23 
GeneralRe: My vote of 1 Pin
Shweta Lodha21-May-12 3:28
Shweta Lodha21-May-12 3:28 
GeneralRe: My vote of 1 Pin
Andreas Gieriet21-May-12 9:34
professionalAndreas Gieriet21-May-12 9:34 
GeneralRe: My vote of 1 Pin
Shivprasad koirala23-May-12 4:29
Shivprasad koirala23-May-12 4:29 
GeneralRe: My vote of 1 Pin
Shweta Lodha23-May-12 5:05
Shweta Lodha23-May-12 5:05 
General[My vote of 1] this is nothing new Pin
Seishin#20-May-12 21:17
Seishin#20-May-12 21:17 
GeneralRe: [My vote of 1] this is nothing new Pin
Shweta Lodha21-May-12 3:23
Shweta Lodha21-May-12 3:23 
GeneralRe: [My vote of 1] this is nothing new Pin
PIEBALDconsult21-May-12 7:31
mvePIEBALDconsult21-May-12 7:31 
GeneralRe: [My vote of 1] this is nothing new Pin
Shweta Lodha21-May-12 18:14
Shweta Lodha21-May-12 18:14 
yes, how does that matter. Every company have their own roles and designation. Last year, I served as an consultant also.
Regards,
Shweta Jain
http://shwetamannjain.blogspot.in/

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.