String and String Builder

String and String Builder


I‘ll explain and compare two string related objects in the .net framework

• String object
• String builder Object

Major difference we often say that string is mutable and string builder is immutable.

What does it actually mean?

String object qualifies as being called immutable, if its value cannot be modified once it has been created. No characters can be added or removed from it, nor can its length be changed.


Scenario:1

Consider the code below

        string companyName;
        companyName = "Microsoft";
        companyName = companyName + "Welcomes You";

Here three objects are created.once while declaring companyname,once on the Microsoft Statement, and again on the third statemant, which appends the word “welcomes you “ and completely return a new string. This looks simple.but when we have a long list of strings to append.it slows down the performance
Scenario:2

For instance, the code

        Name = Name.Replace("Microsoft", "Nexgen");

doesn’t change the contents of the string that s originally referred to – it just sets the value of s to a new string, which is a copy of the old string but with ” Microsoft ” replaced by ” Nexgen“.

String builder class in found in System. Text namespace
String builder is mutable because it allows you to change the content you have already placed. When you have huge amount of concatenation, best recommendation is to go for String Builder.
Lets try to prove this programmatically.

lets check it out

    const int Length = 30, Loops = 3000;
    DateTime startTime, EndTime;
    int i;
   

        // Time string concatenation.

        StartTime = DateTime.Now;
        string strChk = string.Empty;

        //Response.Write("Start time:" + StartTime.ToString());

        for (i = 0; i < Loops; i++)
        {
            strChk += i.ToString();
        }

        EndTime = DateTime.Now;
        //Response.Write("End time:" + EndTime.ToString());
      
        Response.Write("Concatenation took " + (EndTime - StartTime).TotalSeconds + " seconds.");
   
        // Time StringBuilder.

        StartTime = DateTime.Now;
        //Response.Write("Start time:" + StartTime.ToString());

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (i = 0; i < Loops; i++)
        {
            sb.Append(i.ToString());
        }
     
        EndTime = DateTime.Now;
        //Response.Write("End time:" + EndTime.ToString());
        Response.Write("String Builder took " + (EndTime - StartTime).TotalSeconds + " seconds.");

From this we can see that string builder shows better performance than string.

If you are not modifying your strings or if you are concatenating small numbers, you might leave your strings as they are. If, on the other hand, you are performing many manipulations, you might be better off changing your String instances to StringBuilders. By following this approach, you would be assured that your application will not suffer performance degradation due to String manipulation.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: