stringbuilder和string拼字符串的性能,有点吓人。
代码
System.Text.StringBuilder sb = new System.Text.StringBuilder();
int i = 0;
long StartTime = DateTime.Now.Ticks;
while (i < 80000)
{
sb.Append(i.ToString());
i++;
}
long EndTime = DateTime.Now.Ticks;
Console.WriteLine("时间:" + (EndTime - StartTime) + "\t Capacity:" + sb.Capacity + "\t Length:"
+ sb.Length);
string sb1 = null;
i = 0;
StartTime = DateTime.Now.Ticks;
while (i < 80000)
{
sb1 += i;
i++;
}
EndTime = DateTime.Now.Ticks;
Console.WriteLine("时间:" + (EndTime - StartTime));
Console.Read();
int i = 0;
long StartTime = DateTime.Now.Ticks;
while (i < 80000)
{
sb.Append(i.ToString());
i++;
}
long EndTime = DateTime.Now.Ticks;
Console.WriteLine("时间:" + (EndTime - StartTime) + "\t Capacity:" + sb.Capacity + "\t Length:"
+ sb.Length);
string sb1 = null;
i = 0;
StartTime = DateTime.Now.Ticks;
while (i < 80000)
{
sb1 += i;
i++;
}
EndTime = DateTime.Now.Ticks;
Console.WriteLine("时间:" + (EndTime - StartTime));
Console.Read();