日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
四種方法統(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


網(wǎng)頁題目:四種方法統(tǒng)計(jì)字符串的行數(shù)和執(zhí)行時(shí)間比較
URL網(wǎng)址:http://www.dlmjj.cn/article/coesphi.html