473,416 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,416 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 5418
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

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

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
2
by: Zach Tong | last post by:
I recently ran our code through a profiler to determine why it was using so much memory. It turns out the System.String object is taking 95% of the memory. We have considered converting the...
8
by: Jami Bradley | last post by:
Hi, I'm looking for an efficient way to do this, because I know it will be heavily used :-) I have a fixed width string and I need to substitute a substring of characters with new values. I...
7
by: ORC | last post by:
Hi, How to transfer strings between a C# .NET application and a native DLL that uses Unicode like: #ifndef UNICODE #define UNICODE #endif The function header in the DLL: BOOL...
4
by: Vinay Agarwal | last post by:
Hello, I would like to maximize efficiency of string processing in my C# application that works with Skype API. Actually, these "strings" are made of "chars" that are all 8-bit values except 0...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
26
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW:...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.