StringBuilder append Vs String Concatenation "+"
For all of you looking for proof that Stringbuilder is faster than the normal string concatenation using "+"
In the following code snippet i have used both the methods and used start time and end time in each method to calculate the Elasped time for each respective method Create a new project and add the necessary labels and the button to check it for yourself.
private void Calculatebutton_Click(object sender, System.EventArgs e)
{
DateTime plusStart;
DateTime plusEnd;
DateTime bldrStart;
DateTime bldrEnd;
string test = String.Empty;
string stringToAppend = "ap";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
int upperlimit = 100000;
plusStart = DateTime.Now;
for (int i=0 ; i < upperlimit ; i ++ )
{
test += stringToAppend;
}
plusEnd = DateTime.Now;
plusElaspedlabel.Text = plusEnd.Subtract(plusStart).ToString();
plusEndlabel.Text = plusEnd.ToString();
plusStartlabel.Text = plusStart.ToString();
bldrStart = DateTime.Now;
for (int j=0; j < upperlimit ; j ++ )
{
sb.Append(stringToAppend);
}
bldrEnd = DateTime.Now;
bldrElaspedlabel.Text = bldrEnd.Subtract(bldrStart).ToString();
bldrEndlabel.Text = bldrEnd.ToString();
bldrStartlabel.Text = bldrStart.ToString();
}
In the following code snippet i have used both the methods and used start time and end time in each method to calculate the Elasped time for each respective method Create a new project and add the necessary labels and the button to check it for yourself.
private void Calculatebutton_Click(object sender, System.EventArgs e)
{
DateTime plusStart;
DateTime plusEnd;
DateTime bldrStart;
DateTime bldrEnd;
string test = String.Empty;
string stringToAppend = "ap";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
int upperlimit = 100000;
plusStart = DateTime.Now;
for (int i=0 ; i < upperlimit ; i ++ )
{
test += stringToAppend;
}
plusEnd = DateTime.Now;
plusElaspedlabel.Text = plusEnd.Subtract(plusStart).ToString();
plusEndlabel.Text = plusEnd.ToString();
plusStartlabel.Text = plusStart.ToString();
bldrStart = DateTime.Now;
for (int j=0; j < upperlimit ; j ++ )
{
sb.Append(stringToAppend);
}
bldrEnd = DateTime.Now;
bldrElaspedlabel.Text = bldrEnd.Subtract(bldrStart).ToString();
bldrEndlabel.Text = bldrEnd.ToString();
bldrStartlabel.Text = bldrStart.ToString();
}