偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

四種方法統(tǒng)計字符串的行數(shù)和執(zhí)行時間比較

開發(fā) 開發(fā)工具
在開發(fā)過程中我需要統(tǒng)計一下字符串的行數(shù),因此我就寫了一個超級沒有技術(shù)含量的蠻力方法來統(tǒng)計了。最后我將進行一個執(zhí)行時間的比較。

我需要統(tǒng)計一下字符串的行數(shù),因此我就寫了一個超級沒有技術(shù)含量的蠻力方法來統(tǒng)計了。

  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.   } 

  這個函數(shù)他呀,運行正常,寫起來也快。

  但是,我就像啊,這是不是也太沒有技術(shù)含量了,難道就沒有其他方法了?

  當然有,我想出了兩種方法:正則和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.   } 

  然后呢,我又寫了一個快速但混亂的毫無技術(shù)含量的測試程序來測試正確性

 

  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行。

  測試結(jié)果顯示是

  22777:22777:22777

  有意思的是這個執(zhí)行時間的結(jié)果(ms計)

  Test ElapsedMilliseconds

  BF+I 181

  Split 1089

  Regex 2557

  Linq 3590

  我本來想著這正則要快的不是一點點啊。正則和Linq這么大的差異令我震驚了,最令我震驚的是BF+I竟然比他們兩個都快,而分割則毫無疑問比Index要慢,因為在分割方法中.net一次要分配23k的字符串空間

  為了完成任務(wù),我把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系統(tǒng) JEECMS v2012版發(fā)布
  2. Java生成樹結(jié)構(gòu)各點之間最短路徑算法
  3. Java 遠程文件對接
  4. Java I/O系統(tǒng)基礎(chǔ)知識
  5. Ubuntu對Java開發(fā)包說再見!
責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2020-07-14 08:17:26

代碼執(zhí)行時間

2011-06-22 15:21:08

XML

2010-09-08 15:00:03

SQL語句執(zhí)行

2014-03-17 09:22:43

Linux命令

2022-09-02 14:29:01

JavaScrip數(shù)組屬性

2011-08-22 09:54:40

云計算虛擬化云成本

2009-02-25 09:52:14

類型轉(zhuǎn)換.NET 強制轉(zhuǎn)型

2009-03-31 13:12:30

解析XMLJava

2020-08-10 00:30:55

備份密碼iPhone移動安全

2009-11-23 15:57:51

PHP偽靜態(tài)

2021-03-10 10:13:39

爬蟲Python代碼

2010-11-18 15:53:30

Oracle語句執(zhí)行時

2018-07-18 15:13:56

MCU代碼時間

2021-02-24 11:44:35

語言計算函數(shù)嵌入式系統(tǒng)

2009-09-17 16:55:58

C#組件設(shè)計

2010-08-02 16:47:46

Flex

2020-01-21 19:15:23

漏洞安全IT

2020-07-24 09:56:12

React開發(fā)數(shù)據(jù)

2021-09-03 11:24:04

云計算云計算環(huán)境云應(yīng)用

2014-02-28 10:50:24

Linux命令
點贊
收藏

51CTO技術(shù)棧公眾號