字符串连接性能
2015-10-29 16:37
134 查看
class Program { private static readonly string STR = "0123456789"; private static string longSTR = StringList(1024, STR); static void Main(string[] args) { Test(10000, STR); Test(10, longSTR); } public static void Test(int count, string str) { for (int i = 2; i <= 1024; i *= 2) { Time(string.Format("Nomal Concat ({0})", i), count, () => Concat(i, str)); Time(string.Format("StringBulider ({0})", i), count, () => StringBulider(i, str)); Time(string.Format("String List ({0})", i), count, () => StringList(i, str)); Console.WriteLine(); } } public static void Time(string name, int count, Func<string> function) { var watch = new Stopwatch(); watch.Start(); for (int i = 0; i < count; i++) { function(); } watch.Stop(); Console.WriteLine(name + watch.ElapsedTicks); watch.Reset(); } public static string Concat(int count, string str) { var result = string.Empty; for (int i = 0; i < count; i++) { result += str + "|"; } return result; } public static string StringBulider(int count, string str) { var result = new StringBuilder(); for (int i = 0; i<count; i ++) { result.Append(str + "|"); } return result.ToString(); } public static string StringList(int count, string str) { var result = new List<string>(); for (int i = 0; i < count; i++) { result.Add(str); } return string.Join("|", result); } }
相关文章推荐
- 100个iOS开发面试题汇总-王刚韧的技术博客
- 简单区分Vmware的三种网络连接模式(bridged、NAT、host-only)
- centos系统无法使用ssh远程工具连接方法
- 8.2 使用计时器的三种方法
- jQuery实现切换页面过渡动画效果
- Linux 线程属性函数总结
- UVa 11831 - Sticker Collector Robot
- shell 字符串(六)
- Leetcode -- Sliding Window Maximum
- Excel VBA常用代码总结1
- 一步一步开发电商平台 优惠活动
- 百度认为什么样的网站更有抓取和收录价值
- android开发Fragment not attached to Activity异常解决
- Java中的匿名内部类
- HTTP报文内的HTTP信息之获取部分内容的范围请求
- Android内存泄漏检测-LeakCanary
- IOS开发中,SVN如何恢复到某一个版本(以Cornerstone为例)
- leetcode 298: Binary Tree Longest Consecutive Sequence
- 技术博客网站推荐
- Android如何更新app的版本(中级)