Click here to Skip to main content
Click here to Skip to main content

Performance analysis for String and StringBuilder

, 23 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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:

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.

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.

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)

Share

About the Author

Shweta Lodha
Technical Lead
India India
You can also follow me at my blog http://www.shwetalodha.blogspot.in/
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 PinmemberLokeshZende30-Jul-12 0:52 
GeneralMy vote of 4 Pinmemberpratapvarun2-Jun-12 7:40 
GeneralMy vote of 4 Pinmemberhoneysinghi23-May-12 18:17 
QuestionIts nice just that the location is wrong. PinmemberShivprasad koirala23-May-12 5:27 
AnswerRe: Its nice just that the location is wrong. PinmemberShweta Lenka23-May-12 5:30 
GeneralThoughts PinmemberPIEBALDconsult21-May-12 8:39 
GeneralMy vote of 1 PinmemberAndreas Gieriet20-May-12 22:23 
GeneralRe: My vote of 1 PinmemberShweta Lenka21-May-12 4:28 
GeneralRe: My vote of 1 PinmemberAndreas Gieriet21-May-12 10:34 
GeneralRe: My vote of 1 PinmemberShivprasad koirala23-May-12 5:29 
Every new comer who writes articles goes through this phase.
 
If you can do the following
 
-- This is more of a blog and not a tip and trick
-- The heading is pointing straight to your blog , your blog is already referenced below.
My book .NET interview questions with 500 mostly asked questions in .NET world .NET Interview questions and answers

GeneralRe: My vote of 1 PinmemberShweta Lenka23-May-12 6:05 
General[My vote of 1] this is nothing new PinmemberSeishin#20-May-12 22:17 
GeneralRe: [My vote of 1] this is nothing new PinmemberShweta Lenka21-May-12 4:23 
GeneralRe: [My vote of 1] this is nothing new PinmemberPIEBALDconsult21-May-12 8:31 
GeneralRe: [My vote of 1] this is nothing new PinmemberShweta Lenka21-May-12 19:14 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 23 May 2012
Article Copyright 2012 by Shweta Lodha
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid