av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

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

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

專注于為中小企業(yè)提供做網(wǎng)站、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)姜堰免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

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

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

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

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

  然后呢,我又寫了一個(gè)快速但混亂的毫無技術(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

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

  Test ElapsedMilliseconds

  BF+I 181

  Split 1089

  Regex 2557

  Linq 3590

  我本來想著這正則要快的不是一點(diǎn)點(diǎn)啊。正則和Linq這么大的差異令我震驚了,最令我震驚的是BF+I竟然比他們兩個(gè)都快,而分割則毫無疑問比Index要慢,因?yàn)樵诜指罘椒ㄖ?net一次要分配23k的字符串空間

  為了完成任務(wù),我把BF+I版本重寫了一個(gè)類,并且判斷了字符串只有一行的情況,如你期望的一樣,不要一秒就完成了

 
 
 
  1.   staticclassExtensionMethods  
  2.   {  
  3.   ///  
  4.   ///Returns the number of lines in a string///  
  5.   ///  
  6.   ///  
  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后,時(shí)間更短了一些。

  Test ElapsedMilliseconds

  BF+I 170

  Split 1089

  Regex 2063

  Linq 3583

  完成。。

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


本文名稱:四種方法統(tǒng)計(jì)字符串的行數(shù)和執(zhí)行時(shí)間比較
文章地址:http://uogjgqi.cn/article/coesphi.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們在微信上24小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流