刚才看到一个关于字符串反转的例子
2010-03-04 17:44
375 查看
刚才看到一篇关于字符串反转的文章,原文地址
http://www.cnblogs.com/Mars_Chen/archive/2010/03/04/1678123.html
当时第一感觉是用Array类的reverse方法多好,何必用正则这么麻烦呢?要知道正则虽然看起来简单,但是真正匹配起来可也不容易,于是便一时好奇把这两种方法比较了一下,谁知不比不知道,一比吓一跳啊。下面看代码和结果
代码
执行结果
My Method --- Each time:00:00:00.0304430
My Method --- Each time:00:00:00.0258621
My Method --- Each time:00:00:00.0243496
My Method --- Each time:00:00:00.0272635
My Method --- Each time:00:00:00.0394696
My Method --- Each time:00:00:00.0242083
My Method --- Each time:00:00:00.0291635
My Method --- Each time:00:00:00.0289069
My Method --- Each time:00:00:00.0243922
My Method --- Each time:00:00:00.0263951
My Method --- Avg time:28
Your Method --- Each time:00:00:01.6769779
Your Method --- Each time:00:00:01.6586320
Your Method --- Each time:00:00:01.6922838
Your Method --- Each time:00:00:01.6572740
Your Method --- Each time:00:00:01.6599065
Your Method --- Each time:00:00:01.6630039
Your Method --- Each time:00:00:01.7182288
Your Method --- Each time:00:00:01.9108913
Your Method --- Each time:00:00:01.7913726
Your Method --- Each time:00:00:01.7917344
Your Method --- Avg time:1722
http://www.cnblogs.com/Mars_Chen/archive/2010/03/04/1678123.html
当时第一感觉是用Array类的reverse方法多好,何必用正则这么麻烦呢?要知道正则虽然看起来简单,但是真正匹配起来可也不容易,于是便一时好奇把这两种方法比较了一下,谁知不比不知道,一比吓一跳啊。下面看代码和结果
代码
static void Main(string[] args) { Stopwatch sw = new Stopwatch(); Stopwatch swavg = new Stopwatch(); swavg.Start(); for (int j = 0; j < 10; j++) { sw.Start(); for (int i = 0; i < 100000; i++) { Reverse1("Who will be faster? Yours or mine? Do you really mind my test?"); } sw.Stop(); Console.WriteLine("My Method --- Each time:" + sw.Elapsed.ToString()); sw.Reset(); } swavg.Stop(); Console.WriteLine("My Method --- Avg time:" +(sw.ElapsedMilliseconds/10).ToString()); swavg.Reset(); swavg.Start(); for (int j = 0; j < 10; j++) { sw.Start(); for (int i = 0; i < 100000; i++) { Reverse2("Who will be faster? Yours or mine? Do you really mind my test?"); } sw.Stop(); Console.WriteLine("Your Method --- Each time:" + sw.Elapsed.ToString()); sw.Reset(); } swavg.Stop(); Console.WriteLine("Your Method --- Avg time:" + (sw.ElapsedMilliseconds / 10).ToString()); Console.ReadLine(); } static string Reverse1(string original) { char[] arr = original.ToCharArray(); Array.Reverse(arr); return new string(arr); } static string Reverse2(string original) { string result = ""; MatchCollection mc = Regex.Matches(original, @"(\S)+"); for (int i = mc.Count - 1; i >= 0; i--) { result += mc[i].Value; } return result; }
执行结果
My Method --- Each time:00:00:00.0304430
My Method --- Each time:00:00:00.0258621
My Method --- Each time:00:00:00.0243496
My Method --- Each time:00:00:00.0272635
My Method --- Each time:00:00:00.0394696
My Method --- Each time:00:00:00.0242083
My Method --- Each time:00:00:00.0291635
My Method --- Each time:00:00:00.0289069
My Method --- Each time:00:00:00.0243922
My Method --- Each time:00:00:00.0263951
My Method --- Avg time:28
Your Method --- Each time:00:00:01.6769779
Your Method --- Each time:00:00:01.6586320
Your Method --- Each time:00:00:01.6922838
Your Method --- Each time:00:00:01.6572740
Your Method --- Each time:00:00:01.6599065
Your Method --- Each time:00:00:01.6630039
Your Method --- Each time:00:00:01.7182288
Your Method --- Each time:00:00:01.9108913
Your Method --- Each time:00:00:01.7913726
Your Method --- Each time:00:00:01.7917344
Your Method --- Avg time:1722
相关文章推荐
- 在网上看到的一个关于开闭原则的例子
- 一个关于字符串截取输出的例子
- 一个字符串反转的小例子
- 刚才看到的一个小坑。 关于获取元素透明度
- 刚才看到一个关于[伪原创]的文章,反正我推荐了
- 今天看到的一个犀利的带符号字符串一起反转的代码
- 在网上看到了一个面试题感觉很有意思 写一个函数,实现字符串反转
- 一个关于字符串反转的问题;
- 关于字符串操作的一个小例子(递归实现)
- 关于ALV双击 调用事务的一个例子
- 一个关于委托事件的例子(计时器)
- 关于String操作的一个小例子
- 关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 读取文件
- 一个关于ACE Service Configurator的小例子
- 关于ExpandableListView用法的一个简单小例子
- 将一个字符串反转的三种方法
- 一个有限状态机判断字符串是否数字的例子
- 数组不是指针——数组地址不能动态分配空间,一个小例子关于指针移动,以及malloc
- 一个误区(关于javascript的字符串拼接)
- 一个关于页面右侧浮层相对定位的例子