The Artima Developer Community
Sponsored Link

.NET Buzz Forum
String VS. StringBuilder

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Michael Mello

Posts: 100
Nickname: knarf
Registered: May, 2004

Michael Mello is .NET Web Developer
String VS. StringBuilder Posted: May 3, 2004 6:00 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Michael Mello.
Original Post: String VS. StringBuilder
Feed Title: melloblog
Feed URL: http://www.thauvin.net/errorpage.htm?aspxerrorpath=/Default.aspx
Feed Description: .NET and Everything After.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Michael Mello
Latest Posts From melloblog

Advertisement
The String class in .NET is extremely powerful, and quite useful, but there are times when you want to avoid it.  A good reason to steer clear of the String class is when you're manipulating a large number of strings.  Instead of doing the usual concatenation of String objects, it's better to use the StringBuilder class.

Differences of String and StringBuilder

The string is an immutable data type, which means that once a string object is created, it cannot be changed.  So when you're concatenating a string, you're really allocating memory for a whole new string instance.  On the other hand, StringBuilder will allocate more memory for your string then is needed.  All string modifications using the StringBuilder class, will take place in this block of memory, which results in better efficiency.

String VS. StringBuilder Example

Sure, all this efficiency talk is nice, but how about a concrete example to back it up?  The following is a simple C# console program I whipped up, which displays the results of concatenating 20,000 times, through both the String and StringBuilder classes.


static void Main(string[] args)
{
string test1 = "Hello World";
StringBuilder test2 = new StringBuilder("Hello World");


Console.WriteLine("Concatenating 'Hello World' 20000 times using String.");
Console.WriteLine("Start Time: " + DateTime.Now.ToString());
for (int x=0; x<= 20000; x++)
{
test1 += "Hello World";

}
Console.WriteLine("Finish Time: " + DateTime.Now.ToString());
Console.WriteLine();

Console.WriteLine("Concatenating 'Hello World' 20000 times using StringBuilder.");
Console.WriteLine("Start Time: " + DateTime.Now.ToString());
for (int x=0; x<= 20000; x++)
{
test2.Append("Hello World");

}
Console.WriteLine("Finish Time: " + DateTime.Now.ToString());
Console.ReadLine();
}



Results

Concatenating 'Hello World' 20000 times using String.
Start Time: 5/3/2004 8:24:05 PM
Finish Time: 5/3/2004 8:24:16 PM

Concatenating 'Hello World' 20000 times using StringBuilder.
Start Time: 5/3/2004 8:24:16 PM
Finish Time: 5/3/2004 8:24:16 PM


Conclusion

As you can see from the results, there is an eleven second difference between the two.  Although StringBuilder is very efficient, you don't want to use it during simple string manipulation, as it's more overhead than you need.  Feel free to download the code, and use it for your own testing purposes - just keep in mind, that I'm running a P4 2.4ghz, so results will vary.


String VS StringBuilder Example

Read: String VS. StringBuilder

Topic: Free your mind - a totally different way to learn about [Web services] Previous Topic   Next Topic Topic: .NET developers blog

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use