通常的答案是,字符串连接对于4到8个字符串更有效。这取决于您阅读的博客。
不要编写测试来决定使用哪种方法。如果您不确定它是否会超出魔术限制,则只需使用StringBuilder。
运行以下代码以亲自查看结果:
const int sLen=30, Loops=5000;DateTime sTime, eTime;int i;string sSource = new String(’X’, sLen);string sDest = '';// // Time string concatenation.// sTime = DateTime.Now;for(i=0;i<Loops;i++) sDest += sSource;eTime = DateTime.Now;Console.WriteLine('Concatenation took ' + (eTime - sTime).TotalSeconds + ' seconds.');// // Time StringBuilder.// sTime = DateTime.Now;System.Text.StringBuilder sb = new System.Text.StringBuilder((int)(sLen * Loops * 1.1));for(i=0;i<Loops;i++) sb.Append(sSource);sDest = sb.ToString();eTime = DateTime.Now;Console.WriteLine('String Builder took ' + (eTime - sTime).TotalSeconds + ' seconds.');// // Make the console window stay open// so that you can see the results when running from the IDE.// Console.WriteLine();Console.Write('Press Enter to finish ... ');Console.Read();
参考http://support.microsoft.com/kb/306822
解决方法我有一种情况,我需要连接多个字符串以形成一个类的ID。基本上,我只是循环访问列表以获取对象的ToString值,然后将它们串联。
foreach (MyObject o in myList) result += o.ToString();
列表中不应包含5个以上的元素(尽管可以,但是这是一个非常非常微不足道的情况),通常包含1到3个元素,通常只有一个或两个。
保持连接或使用StringBuilder会有什么更好的性能?
StringBuilder bld = new StringBuilder()foreach (MyObject o in myList) bld.Append(o.ToString());
我不确定在大多数情况下创建StringBuilder是否比标准串联花费更多时间。
这是懒惰的,列表中的项目一旦创建就不会更改,因此,一次调用就懒惰地构造了id。
附带说明…我应该使用固定数组而不是列表?如果可以,我会获得任何性能或内存改进吗?(无论如何,列表仅用作IEnumerable)
关于这个问题的更一般的看法可能是,有多少个字符串足以停止连接并开始构建?
我是否应该花时间去测试这种情况?
if (myList.Count > 4) ConcatWithStringBuilder(myList);