The difference between String and StringBuilder
Written: 2010-02-05 06:55:54String s = "Hello" + "World" + "!";
int[] myInts = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
myInts[10] = 11; //This will cause a runtime error
int[] myInts = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//myInts[10] = 11;
int[] myNewInts = new int[11];
for (int i = 0; i < 10; i++)
{
myNewInts[italic] = myInts[italic];
}
myNewInts[10] = 11;
String container = "";
int count = 0;
for (int i = 0; i < 10000; i++)
{
container += "Hello World!";
count++;
}
using System;
using System.Text;
using System.Windows.Forms;
namespace StringTest
{
public partial class Form1 : Form
{
String insertString = "Hello World!";
int stringAttempts;
int stringBuilderAttempts;
public Form1()
{
InitializeComponent();
stringAttempts = 1;
stringBuilderAttempts = 1;
}
private void btnString_Click(object sender, EventArgs e)
{
String container = "";
DateTime startTime = DateTime.Now;
int count = 0;
for (int i = 0; i < 10000; i++)
{
container += insertString;
count++;
}
DateTime stopTime = DateTime.Now;
TimeSpan elapsedTime = stopTime - startTime;
txtOutput.Text += "String experiment " + stringAttempts.ToString() + Environment.NewLine;
txtOutput.Text += "The phrase \"Hello World!\" was added " + count.ToString() + " times." + Environment.NewLine;
txtOutput.Text += "Elapsed time: " + elapsedTime.ToString() + Environment.NewLine;
txtOutput.Text += "_______________________________________________________" + Environment.NewLine + Environment.NewLine;
stringAttempts++;
}
private void btnStringBuilder_Click(object sender, EventArgs e)
{
StringBuilder container = new StringBuilder();
DateTime startTime = DateTime.Now;
int count = 0;
for (int i = 0; i < 10000; i++)
{
container.Append(insertString);
count++;
}
DateTime stopTime = DateTime.Now;
TimeSpan elapsedTime = stopTime - startTime;
txtOutput.Text += "StringBuilder experiment " + stringBuilderAttempts.ToString() + Environment.NewLine;
txtOutput.Text += "The phrase \"Hello World!\" was added " + count.ToString() + " times." + Environment.NewLine;
txtOutput.Text += "Elapsed time: " + elapsedTime.ToString() + Environment.NewLine;
txtOutput.Text += "_______________________________________________________" + Environment.NewLine + Environment.NewLine;
stringBuilderAttempts++;
}
}
}