Difference between String vs. String builder
String - Once the string object is created, its length and content cannot be modified. This method is slower.
String builder - Once the object is created, it can able to modify length and content. This method is faster.
Example on String vs. String builder
namespace stringvsstringbuilder
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("String Example");
string str = string.Empty;
DateTime start = DateTime.Now;
Console.WriteLine("Start time" + start.ToString());
for (long i = 0; i < 100000; i++)
{
str += i.ToString();
}
DateTime stop = DateTime.Now;
Console.WriteLine("Start time" + stop.ToString());
Console.WriteLine("StringBuilder Example");
StringBuilder builder = new StringBuilder();
start = DateTime.Now;
Console.WriteLine("Start time:" + start.ToString());
for (long i = 0; i < 100000; i++)
{
builder.Append(i.ToString());
}
stop = DateTime.Now;
Console.WriteLine("Stop time:" + stop.ToString());
Console.Read();
}
}
}
No comments:
Post a Comment