乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      統(tǒng)計一個大文本行數(shù)的幾種方法以及效率統(tǒng)計(一)

       ruiruiruiruichen 2013-08-28



      最近在和一個朋友的交流中,遇到這么一個問題,如何能較快對一個較大的文本文件(1G或更大)的文本行數(shù)進行統(tǒng)計。如果不考慮效率,要統(tǒng)計一個文本的行數(shù)其實一點也不難,但是如果需要在較快的時間內(nèi)做完,恐怕就得考慮實現(xiàn)方法了。


      為此,自己嘗試了幾種方法,在這里把這幾種方法拿出來和大家討論一下。


      首先是生成測試數(shù)據(jù)的代碼:


      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      const int COL_NUM = 30;
      const int LINE_NUM = 10000000;
      const string FILE_NAME = @"d:\test.csv";
      /// <summary>
      /// 創(chuàng)建一個一千萬行的文本文件,大小約為900M。
      /// </summary>
      static void CreateTestCSVFile()
      {
          string rowValue = string.Join(string.Empty,
              Enumerable.Range(1, COL_NUM).Select(i => i.ToString("00") + ","));
           
          using (StreamWriter sw = new StreamWriter(FILE_NAME, false, Encoding.ASCII))
          {
              for (int i = 0; i < LINE_NUM; i++)
              {
                  sw.WriteLine(rowValue);
              }
          }
      }

      .NET4.0 + StreamReader + ReadLine()


      原理很簡單,使用StreamReader的ReadLine方法,每執(zhí)行一次,行數(shù)加一。代碼如下:


      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      /// <summary>
      /// StreamReader + ReadLine()
      /// </summary>
      static void CalculateLine_ReadLine()
      {
          long lineCount = 0;
          using (StreamReader sr = new StreamReader(FILE_NAME, false))
          {
              while (!sr.EndOfStream)
              {
                  sr.ReadLine();
                  lineCount++;
              }
          }
          Console.WriteLine("line count: {0}", lineCount);
      }

      測試結(jié)果如下:


      StreamReader ReadLine()


      對于以上這種方法,平均每次執(zhí)行時間為55s左右,執(zhí)行效率明顯不盡如人意。


      如果我們同樣采用流,不過使用分塊的方式,將文件內(nèi)容一塊一塊讀進內(nèi)存,在解析每塊內(nèi)容的行數(shù),最后相加。這樣做的效率如何呢?


      .NET4.0 + StreamReader + ReadBlock()


      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      /// <summary>
      /// StreamReader + ReadBlock()
      /// </summary>
      static void CalculateLine_ReadBlock(int oneBlockSize)
      {
           
          long lineCount = 0;
          char[] oneBlock = new char[oneBlockSize];
          using (StreamReader sr = new StreamReader(FILE_NAME, false))
          {
              long streamLen = sr.BaseStream.Length;
              while (!sr.EndOfStream)
              {
                  long leftLength = streamLen - sr.BaseStream.Position - 1;
                  if (leftLength >= oneBlockSize)
                  {
                      sr.ReadBlock(oneBlock, 0, oneBlock.Length);
                      lineCount += oneBlock.Count(c => c == '\r');
                  }
                  else
                  {
                      lineCount += sr.ReadToEnd().Count(c => c == '\r');
                  }
              }
          }
          Console.WriteLine("line count: {0}", lineCount);
      }
      static void Main(string[] args)
      {
          int[] oneBlockArray = new[] { 1, 10, 20, 50, };
          foreach (int oneBlockSize in oneBlockArray)
          {
               
              CodeTimerF2.CodeTimer.Time(string.Format("StreamReader + ReadBolck() [Block Size: {0}MB]", oneBlockSize),
                  1, () => CalculateLine_ReadBlock(oneBlockSize * 1024 * 1024));
          }
           
          Console.ReadKey();
      }

      運行結(jié)果如圖:


      StreamReader ReadBlock()

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多