StringBuilder ?
using System;using System.Text;
class Test_StringBuilder
{
public static void Main()
{
string s1 = "a",s2 = "a",n;
StringBuilder strB = new StringBuilder(s1);
Console.WriteLine("输入循环次数");
n = Console.ReadLine();
DateTime aT1 = DateTime.Now,aT2;
Console.WriteLine("开始时间:{0}",aT1.ToString("HH:mm:ss.ff"));
for (double i = Convert.ToDouble(n); i>0; i--)
{
strB.Append(s2); //比常规的s1 = s1 + s2; 快很多
}
aT2 = DateTime.Now;
Console.WriteLine("结束时间:{0}",aT2.ToString("HH:mm:ss.ff"));
Console.WriteLine("字符个数:{0}",strB.Length);
TimeSpan ts = aT2 - aT1;
Console.WriteLine("差时间:{0}",ts);
}
}
大量循环时,会消耗大量内存。估计是用的递归方法。(常规方式消耗CPU资源)
我想问的是,循环1亿次,一个字符占一个字节,最后结果约有95G ? 还是我的计算方式有问题?