栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

字符串连接与字符串生成器。性能

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

字符串连接与字符串生成器。性能

通常的答案是,字符串连接对于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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/381133.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号