Click here to Skip to main content
13,152,321 members (43,006 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 20 May 2012

Performance analysis for String and StringBuilder

, 23 May 2012
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.


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


About the Author

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
LokeshZende29-Jul-12 23:52
memberLokeshZende29-Jul-12 23:52 
GeneralMy vote of 4 Pin
pratapvarun2-Jun-12 6:40
memberpratapvarun2-Jun-12 6:40 
GeneralMy vote of 4 Pin
honeysinghi23-May-12 17:17
memberhoneysinghi23-May-12 17:17 
QuestionIts nice just that the location is wrong. Pin
Shivprasad koirala23-May-12 4:27
memberShivprasad koirala23-May-12 4:27 
AnswerRe: Its nice just that the location is wrong. Pin
Shweta Lenka23-May-12 4:30
memberShweta Lenka23-May-12 4:30 
GeneralThoughts Pin
PIEBALDconsult21-May-12 7:39
memberPIEBALDconsult21-May-12 7:39 
GeneralMy vote of 1 Pin
Andreas Gieriet20-May-12 21:23
memberAndreas Gieriet20-May-12 21:23 
GeneralRe: My vote of 1 Pin
Shweta Lenka21-May-12 3:28
memberShweta Lenka21-May-12 3:28 
GeneralRe: My vote of 1 Pin
Andreas Gieriet21-May-12 9:34
memberAndreas Gieriet21-May-12 9:34 
GeneralRe: My vote of 1 Pin
Shivprasad koirala23-May-12 4:29
memberShivprasad koirala23-May-12 4:29 
GeneralRe: My vote of 1 Pin
Shweta Lenka23-May-12 5:05
memberShweta Lenka23-May-12 5:05 
General[My vote of 1] this is nothing new Pin
Seishin#20-May-12 21:17
memberSeishin#20-May-12 21:17 
GeneralRe: [My vote of 1] this is nothing new Pin
Shweta Lenka21-May-12 3:23
memberShweta Lenka21-May-12 3:23 
GeneralRe: [My vote of 1] this is nothing new Pin
PIEBALDconsult21-May-12 7:31
memberPIEBALDconsult21-May-12 7:31 
GeneralRe: [My vote of 1] this is nothing new Pin
Shweta Lenka21-May-12 18:14
memberShweta Lenka21-May-12 18:14 

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.

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