Tuesday, July 26, 2011

C# and .Net Interview Questions”- Why String Builders are faster than strings?


Strings: - strings are also called as immutable because every time you
store a new value in string a new copy is created in the memory location.





String builders: - string builders are also called as mutable because
every time you store a new value in string builder

it actually appends the value in the memory location. This means insertion
is done on existing string.

Let’s demonstrate a small example which will give a clear idea on who is faster
string builder or string.

This is very simple three steps procedure as follows.

Step1: -

Create a new console application like below diagrams.


Go to file > New > Project > Console Application and Click Ok.




Step2: -


Import System.Text namespace in your application.

usingSystem.Text;

Step3: -

Write the below code snippet in your main class.

staticvoid Main(string[] args)
{
while (true)
{
Console.WriteLine("*********Begin Test*********");
Console.WriteLine("============================");
Console.WriteLine("Creating string");
Console.WriteLine();//for having a gap in the lines.
List<string>strlist = newList<string>();
for (int i = 0; i < 30000; i++)
{
strlist.Add(i.ToString());
}
Console.WriteLine("Time taken by string to concate....");
Console.WriteLine();
DateTime start = DateTime.Now;
stringConcateString = string.Empty;
foreach (strings instrlist)
{
ConcateString += s + "";
}
TimeSpan time = (DateTime.Now - start);
Console.WriteLine("Total Time : {0} MilliSeconds", time);
Console.WriteLine("============================");
Console.WriteLine("Creating StringBuilder");
Console.WriteLine();
start = DateTime.Now;
StringBuildersb = newStringBuilder();
foreach (strings instrlist)
{
sb.Append(s + "");
}
time = (DateTime.Now - start);
Console.WriteLine("Time taken by StringBuilder to concate....");
Console.WriteLine();
Console.WriteLine("Total Time : {0} MilliSeconds", time);
Console.WriteLine();
Console.WriteLine("**********End Test**********");
Console.ReadLine();
}

Now run your application and you will see the result like below diagram.


In the above result you can see that the time taken by the string to concateis
greater than the time taken by the string builder to concate, this indicates that the string builders performs faster than the strings.


For more information about generic, please watch the below video.



Please click here to see more C# and Dotnet interview questions

Regards,

Visit Authors blog for more Most asked c# interview questions

2 comments:

Dr. Rajesh Rolen said...

Hi Sir,
I am a big fan of you. I have gone through your all interview books for .net, You are my Idol.

One thing i wants to know related to above article is that "Why string is immutable". I know what is "immutable" but i wants to know why it is created so ( i mean why string is created to be immutable)?

Another thing i wants to know that, what string builder is using internally? is it using linked list/ generic list?

Thanks for gr8 articles.

Rajesh Rolen
www.DotNetAcademy.blogspot.com/

Vee Eee Technologies said...

Excellent pieces. Keep posting such kind of information on your blog. I really impressed by your blog.