Test the stringbuilder efficiency example
- 2020-05-30 21:00:12
- OfStack
// test StringBuilder Operating efficiency
public static void Fun2()
{
#region string
string str = " I love programming !";
// provide 1 Group methods and properties that can be used to accurately measure run time.
Stopwatch stopw = new Stopwatch();
// Start or continue to measure the elapsed time of an interval.
stopw.Start();
for (int i = 0; i < 100000; i++)
{
str += "Test";
}
// Stop measuring the elapsed time of an interval.
stopw.Stop();
Console.WriteLine("string Running time: " + stopw.ElapsedMilliseconds.ToString() + " ms ");
#endregion
#region StringBuilder
StringBuilder sbuild = new StringBuilder(" I love programming! ");
stopw.Reset();
stopw.Start();
for (int i = 0; i < 100000; i++)
{
sbuild.Append("Test");
}
// Stop measuring the elapsed time of an interval.
stopw.Stop();
Console.WriteLine("StringBuilder Running time: " + stopw.ElapsedMilliseconds.ToString() + " ms ");
#endregion
#region In the frame type String
String str2 = " I love programming !";
stopw.Reset();
stopw.Start();
for (int i = 0; i < 10000; i++)
{
str2 += "Test";
}
stopw.Stop();
Console.WriteLine("String Running time: " + stopw.ElapsedMilliseconds.ToString() + " ms ");
// Usage advice: for a large number of character operations in a program Like splicing Something like that Try to use StringBuilder
#endregion
}