Connecting Tech Pros Worldwide Help | Site Map

String and Stringbuilder Usage

  #1  
Old January 7th, 2008, 07:01 AM
Member
 
Join Date: Aug 2006
Location: USA
Posts: 92
Everyone use the type String in their daily programming more often. In addition, most of people do a common mistake to choose between String and StringBuilder.

In concatenation of Strings, if we are initially creating a string, say s = "Hello" and then we are appending to it as s = s + " World", we are actually creating two instances of string in memory. Both the original as well as the new string will be stored in the memory because String is immutable. Therefore, every time when we concatenate the new value, a new instance will be made and it will make a great performance loss if concatenation will be done many times. For that matter, all the activities we do to the string are stored in the memory as separate references and it must be avoided as much as possible.

We can use StringBuilder which is very useful in these kind of scenarios. For the example above, using a StringBuilder as, s.Append(" World"); which only stores the value in the original string and no additional reference is created that will definitely give us a performance boost.

For example-

(I) String Action:

Expand|Select|Wrap|Line Numbers
  1.  string str="";
  2. DateTime startTime = DateTime.Now;
  3. Response.Write(("<br>Start time:" + startTime.ToString()));
  4. int i;
  5.  
  6. for(i=0;i<20000;i++)
  7. {
  8.   str += i.ToString()+ "<br>";
  9.  
  10. DateTime EndTime= DateTime.Now;
  11. Response.Write(("<br>End time:" + EndTime.ToString()));
Output:
Start time:3/22/2006 10:23:44 AM
End time:3/22/2006 10:25:08 AM

The above code took 1 minute and 24 Seconds to complete its operation.

(II) String Builder Action:

Expand|Select|Wrap|Line Numbers
  1.  StringBuilder strbuilder = new StringBuilder();
  2. DateTime startTime1 = DateTime.Now;
  3. Response.Write(("<br>Start time:" + startTime1.ToString()));
  4. int i1;
  5.  
  6. for (i1 = 0; i1 < 20000; i1++)
  7. {
  8.   strbuilder.Append(i1 + "<br>");            
  9. }
  10.  
  11. DateTime EndTime1 = DateTime.Now;
  12. Response.Write(("<br>End time:" + EndTime1.ToString()));
Output:
Start time:3/22/2006 10:25:08 AM
End time:3/22/2006 10:25:09 AM

The above code took 1 Second to complete its operation.

So remember, when we do not want to concatenate then surely use the String and when we have to deal with multiple concatenations, then definitely use StringBuilder.

Thanks &Regs
Bharath Reddy VasiReddy

Last edited by r035198x; July 25th, 2008 at 08:56 AM. Reason: added code tags



  #2  
Old January 23rd, 2008, 12:22 PM
Newbie
 
Join Date: Jan 2008
Posts: 1

re: String and Stringbuilder Usage


Nice article to deal with the Performance
  #3  
Old February 29th, 2008, 03:33 PM
Newbie
 
Join Date: Feb 2008
Posts: 13

re: String and Stringbuilder Usage


Thanks for the article!
It does help to clear things up between the two!
  #4  
Old September 29th, 2008, 08:05 AM
Newbie
 
Join Date: Sep 2008
Posts: 2

re: String and Stringbuilder Usage


its really helpfull...
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stringbuilder and SelectCommand =?Utf-8?B?SGFycnkgVg==?= answers 5 June 1st, 2007 09:25 PM
Regex to remove \t \r \n from string morleyc@gmail.com answers 15 May 20th, 2007 08:25 PM
stringBuilder vs string concatenation myth of usage... genc_ymeri answers 33 April 12th, 2006 03:38 PM
WebRequest method usage ThyRock answers 12 November 19th, 2005 02:22 PM