Background
If you want analysis of more performance issues then hit the following links.
Introduction
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().
- static void Main(string[] args)
- {
- Program p = new Program();
-
- p.GetperformanceString(); //Call String Method here
- p.GetPerformanceStringBulider(); // Call stringBuilder method here
- }
The following is a sample of how to analyze the performance of String:
- string str; //Str is declared Globally....
- public void GetperformanceString()
- {
- //Do stuff here....
-
- }
Set the Loop start time.
- string startingTime = DateTime.Now.ToLongTimeString();
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.
- for (int i = 0; i < 100000; i++)
- {
- str += "j"; //Concatenation "j" every time until the Loop run.
- }
Set the loop end time.
- endtime = DateTime.Now.ToLongTimeString(); //Set the end loop time.
Get the loop take time.
- TimeSpan t1 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));
- Console.WriteLine(t1);
The complete method is here.
- string str;
- public void GetperformanceString()
- {
- Console.WriteLine("Don't worry be Relex your System is Ok ☺");
- Console.WriteLine("String Method Call here ");
- string endtime = DateTime.Now.ToLongTimeString();
- string startingTime = DateTime.Now.ToLongTimeString();
- for (int i = 0; i < 100000; i++)
- {
- str += "j";
- }
-
- endtime = DateTime.Now.ToLongTimeString();
- TimeSpan t1 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));
- Console.WriteLine(t1);
-
- }
The following is a sample of how to analyze the performance of StringBuilder.
Empty method like this type.
- StringBuilder StrBuilder=new StringBuilder(); //Make the object of StringBulider StrBulider.
- public void GetPerformanceStringBulider()
- {
-
- }
Set the loop start time.
- 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.
- for (int i = 0; i < 100000000; i++)
- {
-
- }
Append one word to strbulider.
- 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.
- endtime = DateTime.Now.ToLongTimeString();
- TimeSpan t1 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));
- 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.