using System;
using System.Text;
using System.Diagnostics;
public class Program
{
static void Main(string[] args)
{
int max = 10000;
for (int times = 0; times < 5; times++)
{
{
Console.WriteLine("\ntime: {0}", (times+1).ToString());
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < max; i++)
{
string msg = "Your total is ";
msg += "$500 ";
msg += DateTime.Now;
}
sw.Stop();
Console.WriteLine("String +\t: {0}ms", ((int)sw.ElapsedMilliseconds).ToString().PadLeft(6));
}
{
Stopwatch sw = Stopwatch.StartNew();
StringBuilder msg = new StringBuilder();
for (int j = 0; j < max; j++)
{
// msg.Clear(); // .NET 4.0
msg.Remove(0, msg.Length);
msg.Append("Your total is ");
msg.Append("$500 ");
msg.Append(DateTime.Now);
}
sw.Stop();
Console.WriteLine("StringBuilder\t: {0}ms", ((int)sw.ElapsedMilliseconds).ToString().PadLeft(6));
}
}
Console.Read();
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGV4dDsKdXNpbmcgU3lzdGVtLkRpYWdub3N0aWNzOwoKcHVibGljIGNsYXNzIFByb2dyYW0KewogICAgc3RhdGljIHZvaWQgTWFpbihzdHJpbmdbXSBhcmdzKQogICAgewogICAgICAgIGludCBtYXggPSAxMDAwMDsKICAgICAgICBmb3IgKGludCB0aW1lcyA9IDA7IHRpbWVzIDwgNTsgdGltZXMrKykKICAgICAgICB7CiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJcbnRpbWU6IHswfSIsICh0aW1lcysxKS5Ub1N0cmluZygpKTsKICAgICAgICAgICAgICAgIFN0b3B3YXRjaCBzdyA9IFN0b3B3YXRjaC5TdGFydE5ldygpOwogICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtYXg7IGkrKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBzdHJpbmcgbXNnID0gIllvdXIgdG90YWwgaXMgIjsKICAgICAgICAgICAgICAgICAgICBtc2cgKz0gIiQ1MDAgIjsKICAgICAgICAgICAgICAgICAgICBtc2cgKz0gRGF0ZVRpbWUuTm93OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3cuU3RvcCgpOwogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIlN0cmluZyArXHQ6IHswfW1zIiwgKChpbnQpc3cuRWxhcHNlZE1pbGxpc2Vjb25kcykuVG9TdHJpbmcoKS5QYWRMZWZ0KDYpKTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgU3RvcHdhdGNoIHN3ID0gU3RvcHdhdGNoLlN0YXJ0TmV3KCk7CiAgICAgICAgICAgICAgICBTdHJpbmdCdWlsZGVyIG1zZyA9IG5ldyBTdHJpbmdCdWlsZGVyKCk7CiAgICAgICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG1heDsgaisrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIC8vIG1zZy5DbGVhcigpOyAvLyAuTkVUIDQuMAogICAgICAgICAgICAgICAgICAgIG1zZy5SZW1vdmUoMCwgbXNnLkxlbmd0aCk7CiAgICAgICAgICAgICAgICAgICAgbXNnLkFwcGVuZCgiWW91ciB0b3RhbCBpcyAiKTsKICAgICAgICAgICAgICAgICAgICBtc2cuQXBwZW5kKCIkNTAwICIpOwogICAgICAgICAgICAgICAgICAgIG1zZy5BcHBlbmQoRGF0ZVRpbWUuTm93KTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN3LlN0b3AoKTsKICAgICAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJTdHJpbmdCdWlsZGVyXHQ6IHswfW1zIiwgKChpbnQpc3cuRWxhcHNlZE1pbGxpc2Vjb25kcykuVG9TdHJpbmcoKS5QYWRMZWZ0KDYpKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBDb25zb2xlLlJlYWQoKTsKICAgIH0KfQo=