First Look At Performance String and StringBulider: Part 3


If you want analysis of more performance issues then hit the following links.

In this article we aren't comparing String and StringBulider theoretically. We are only focusing on Particle. I have read many times StringBulider is faster rather then String but don't know how much faster. In a small article we will prove which rovides the best performance. Let's go with me.
How to Check performance

I have no special tools for checking performance. Don't worry about the tools, as we know we are Indian and Indian people are famous for imrovising temporary tools (called in Hindi Jugad). In this article we will use for loops. These loops can test the performance and determine which is the best.  
In a console application there are two methods, one is GetperformanceString() and another is GetPerformanceStringBulder(). 
  1. static void Main(string[] args)  
  2. {  
  3.      Program p = new Program();  
  5.      p.GetperformanceString();             //Call String Method here
  6.      p.GetPerformanceStringBulider();      // Call stringBuilder method here

The following is a sample of how to analyze the performance of String:
  1. string str;   //Str is declared Globally....
  2. public void GetperformanceString()  
  3. {  
  4.    //Do stuff here....

 Set the Loop start time. 
  1. string startingTime = DateTime.Now.ToLongTimeString();  //Set the Loop start Time. 
Create the Loop and set the number of how many  times your loop is running. The following loop is running One Million times. Don't worry, this loop takes around Ten Seconds. 
  1. for (int i = 0; i < 100000; i++)   
  2. {  
  3.    str += "j";  //Concatenation "j" every time until the Loop run.

Set the loop end time.
  1. endtime = DateTime.Now.ToLongTimeString(); //Set the end loop time.
Get the loop take time. 
  1. TimeSpan t1 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime)); //Set the take loop time for processing  
  2.             Console.WriteLine(t1); 
The complete method is here.
  1. string str;  
  2. public void GetperformanceString()  
  3. {  
  4.       Console.WriteLine("Don't worry be Relex your System is Ok ☺");  
  5.       Console.WriteLine("String Method Call here ");  
  6.       string endtime = DateTime.Now.ToLongTimeString();  
  7.       string startingTime = DateTime.Now.ToLongTimeString();  
  8.       for (int i = 0; i < 100000; i++)  
  9.       {  
  10.           str += "j";  
  11.       }  
  13.       endtime = DateTime.Now.ToLongTimeString();  
  14.       TimeSpan t1 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));  
  15.       Console.WriteLine(t1);  

The following is a sample of how to analyze the performance of StringBuilder.
Empty method like this type.
  1. StringBuilder StrBuilder=new StringBuilder();  //Make the object of StringBulider StrBulider.
  2. public void GetPerformanceStringBulider()  
  3. {  

Set the loop start time.
  1. string startingTime = DateTime.Now.ToLongTimeString(); 
Declare the loop for the performance check. This loop continues to execute 100 million times.
Note: It takes around ten seconds but that can vary. It depends on your system performance.  
  1. for (int i = 0; i < 100000000; i++)  
  2. {  
  4. }  
Append one word to strbulider. 
  1. StrBuilder.Append("j");  // it's append until loop is running.
Set the end time and get a difference between the Loop start time and the Loop end time. 
  1. endtime = DateTime.Now.ToLongTimeString();  
  2.            TimeSpan t1 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));  
  3.            Console.WriteLine(t1);  
Output our program.
Final word

I hope everything is clear, if you have any questions drop your question in the comment box.
Note: You can check  Live Demo.

Similar Articles