StringBuilder over String to get better performance in .NET

No.of Views801
Bookmarked0 times
Downloads 
Votes0
By  pranay rana   On  24 Feb 2011 04:02:41
Tag : .NET Frameworks , General
I am going to explain the same thing in more detail to give the beginners more clear view about this fact.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

There are no of article and post says that : StringBuilder is more efficient because it does contain a mutable string buffer. .NET Strings are immutable which is the reason why a new string object is created every time we alter it (insert, append, remove, etc.).

In following post I am going to explain the same thing in more detail to give the beginners more clear view about this fact.
I wrote following code as you can see I have defined one string variable and one StringBuilder variable. Here I am appending string to both type of the variable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace StringTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "Pranay";
            s += " rana";
            s += " rana1";
            s += " rana122";
 
            StringBuilder sb = new StringBuilder();
            sb.Append("pranay");
            sb.Append(" rana");
        }
    }
}
 
After the execution of above code
s= "pranay rana rana1 rana12"
sb = "pranay rana"

Now to get in more details what happened when append string I use red-gate .Net Reflector.

Below image shows the IL of the code that I executed above as you can see

  1. When I append string its execute function Concate(str0,str1) to append string.
  2. When I append string using StringBuilder its call’s Append(str) function.



When I clicked  Contact function in reflector it redirect me to the below code.

As you see Concat function take two argument and return string, following steps performed when we execute append with string type

 

  1. Check for the string is null or not
  2. Create string dest and allocate memory for the string
  3. Fill dest string with str0 and str1
  4. Returns dest string, which is new string variable.

 

So this proves that whenever i do operation like concatenation of strings, it creates new strings 'coz of immutable behaviour of strings.


Note:

Above scenario occurs for all the function related to string operation.

When I clicked on Append function in reflector it redirect me to the below code.

Append function takes one argument of type string and returns StringBuilder object , following steps performed when we execute append with StringBuilder type

  1. Get string value from StringBuilder object
  2. Check the its require to allocate memory for the new string we going to append
  3. Allocate memory if require and append string
  4. If not require to allocate memory than append string directly in existing allocated memory
  5. Return StringBuilder object which called function by using this keyword

So it returns same object without creating new one.

Summary

I hope this article help you to understand inner details about the fact why to use StringBuilder over string to get better performance when you do heavy/major string manipulations in your code.

 
Sign Up to vote for this article
 
About Author
 
pranay rana
Occupation-CEO
Company-GMind Solusion
Member Type-Senior
Location-India
Joined date-08 Jan 2011
Home Page-http://pranayamr.blogspot.com
Blog Page-http://pranayamr.blogspot.com
Hey, I am Pranay Rana, working as a Senior Software engineer in mid-size company located in ahmedabad. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 4.3 years now. For me def. of programming is : Programming is something that you do once and that get used by multiple for many years You can visit me on my blog - http://pranayamr.blogspot.com/ StackOverFlow - http://stackoverflow.com/users/314488/pranay My CV :- http://careers.stackoverflow.com/pranayamr
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top