四种方法统计字符串的行数和执行时间比较

开发 开发工具
在开发过程中我需要统计一下字符串的行数,因此我就写了一个超级没有技术含量的蛮力方法来统计了。最后我将进行一个执行时间的比较。

我需要统计一下字符串的行数,因此我就写了一个超级没有技术含量的蛮力方法来统计了。

  1.   staticlongLinesCount(strings)  
  2.   {  
  3.   longcount = 0;  
  4.   intposition = 0;  
  5.   while((position = s.IndexOf(' ', position)) != -1)  
  6.   {  
  7.   count++;  
  8.   position++; //Skip this occurance!  
  9.   }  
  10.   returncount;  
  11.   } 

  这个函数他呀,运行正常,写起来也快。

  但是,我就像啊,这是不是也太没有技术含量了,难道就没有其他方法了?

  当然有,我想出了两种方法:正则和Linq,我把这些方法都写出来

 

  1.   staticlongLinesCountIndexOf(strings)  
  2.   {  
  3.   longcount = 0;  
  4.   intposition = 0;  
  5.   while((position = s.IndexOf(' ', position)) != -1)  
  6.   {  
  7.   count++;  
  8.   position++; //Skip this occurance!  
  9.   }  
  10.   returncount;  
  11.   }  
  12.   staticRegex r = newRegex(" ", RegexOptions.Multiline);  
  13.   staticlongLinesCountRegex(strings)  
  14.   {  
  15.   MatchCollection mc = r.Matches(s);  
  16.   returnmc.Count;  
  17.   }  
  18.   staticlongLinesCountLinq(strings)  
  19.   {  
  20.  return(fromch ins  
  21.   wherech== ' ' 
  22.   selectch).Count();  
  23.   }  
  24.   staticlongLinesCountSplit(strings)  
  25.   {  
  26.   return(s.Split(newchar[] { ' '})).Length;  
  27.   } 

  然后呢,我又写了一个快速但混乱的毫无技术含量的测试程序来测试正确性

 

  1.   strings = File.ReadAllText(@"D:TempMyLargeTextFile.txt");  
  2.   longindex = LinesCountIndexOf(s);  
  3.   longregex = LinesCountRegex(s);  
  4.   longlinq= LinesCountLinq(s);  
  5.   Console.WriteLine("{0}:{1}:{2}", index, regex, linq);  
  6.   Stopwatch si = newStopwatch();  
  7.   Stopwatch sd = newStopwatch();  
  8.   Stopwatch sl = newStopwatch();  
  9.   Stopwatch ss = newStopwatch();  
  10.   si.Start();  
  11.   for(inti = 0;i <100;i++)  
  12.   {  
  13.   index = LinesCountIndexOf(s);  
  14.   }  
  15.   si.Stop();  
  16.   ss.Start();  
  17.   for(inti = 0;i <100;i++)  
  18.   {  
  19.   index = LinesCountSplit(s);  
  20.   }  
  21.   ss.Stop();  
  22.   sd.Start();  
  23.   for(inti = 0;i <100;i++)  
  24.   {  
  25.   index = LinesCountRegex(s);  
  26.   }  
  27.   sd.Stop();  
  28.   sl.Start();  
  29.   for(inti = 0;i <100;i++)  
  30.   {  
  31.   index = LinesCountLinq(s);  
  32.   }  
  33.   sl.Stop(); 

 

  输入的文件是1.64Mb,包含大约23K行。

  测试结果显示是

  22777:22777:22777

  有意思的是这个执行时间的结果(ms计)

  Test ElapsedMilliseconds

  BF+I 181

  Split 1089

  Regex 2557

  Linq 3590

  我本来想着这正则要快的不是一点点啊。正则和Linq这么大的差异令我震惊了,最令我震惊的是BF+I竟然比他们两个都快,而分割则毫无疑问比Index要慢,因为在分割方法中.net一次要分配23k的字符串空间

  为了完成任务,我把BF+I版本重写了一个类,并且判断了字符串只有一行的情况,如你期望的一样,不要一秒就完成了

 

  1.   staticclassExtensionMethods  
  2.   {  
  3.   ///<summary>  
  4.   ///Returns the number of lines in a string///</summary>  
  5.   ///<param name="s"></param>  
  6.   ///<returns></returns>  
  7.   publicstaticlongLines(thisstrings)  
  8.   {  
  9.   longcount = 1;  
  10.   intposition = 0;  
  11.   while((position = s.IndexOf(' ', position)) != -1)  
  12.   {  
  13.   count++;  
  14.   position++; //Skip this occurance!  
  15.   }  
  16.   returncount;  
  17.   }  
  18.   } 

 

  注:count初始为1后,时间更短了一些。

  Test ElapsedMilliseconds

  BF+I 170

  Split 1089

  Regex 2063

  Linq 3583

  完成。。

原文链接:http://www.cnblogs.com/lazycoding/archive/2012/01/09/2317552.html

【编辑推荐】

  1. Java开源CMS系统 JEECMS v2012版发布
  2. Java生成树结构各点之间最短路径算法
  3. Java 远程文件对接
  4. Java I/O系统基础知识
  5. Ubuntu对Java开发包说再见!
责任编辑:彭凡 来源: 博客园
相关推荐

2020-07-14 08:17:26

代码执行时间

2011-06-22 15:21:08

XML

2010-09-08 15:00:03

SQL语句执行

2010-11-18 15:53:30

Oracle语句执行时

2018-07-18 15:13:56

MCU代码时间

2021-02-24 11:44:35

语言计算函数嵌入式系统

2009-11-23 15:57:51

PHP伪静态

2021-03-10 10:13:39

爬虫Python代码

2014-03-17 09:22:43

Linux命令

2022-09-02 14:29:01

JavaScrip数组属性

2009-07-31 14:04:11

C#时间比较大小

2010-09-02 10:02:17

PHP

2020-08-01 16:19:13

JavaScript字符串开发

2011-08-22 09:54:40

云计算虚拟化云成本

2022-11-04 13:35:29

IT远程工作混合工作

2022-12-07 10:28:22

2022-12-19 15:25:22

Linux命令

2016-06-28 10:19:31

云计算云安全

2010-07-16 13:50:53

Perl哈希表

2009-03-31 13:12:30

解析XMLJava
点赞
收藏

51CTO技术栈公众号