471,066 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 471,066 developers and data experts.

String and Stringbuilder Usage

111 100+
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
Jan 7 '08 #1
3 5262
nat85
1
Nice article to deal with the Performance
Jan 23 '08 #2
MedIt
15
Thanks for the article!
It does help to clear things up between the two!
Feb 29 '08 #3
its really helpfull...
Sep 29 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Zach Tong | last post: by
8 posts views Thread by Jami Bradley | last post: by
7 posts views Thread by ORC | last post: by
4 posts views Thread by Vinay Agarwal | last post: by
12 posts views Thread by Richard Lewis Haggard | last post: by
26 posts views Thread by Hardy Wang | last post: by
15 posts views Thread by morleyc | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.