title: Is StreamReader.Readline() really the fastest method to count lines in a file?
body: <p>While looking around for a while I found quite a few discussions on how to figure out the number of lines in a file.</p>
  
  <p>For example these three:<br>
  <a href="https://stackoverflow.com/questions/496663/c-sharp-how-do-i-count-lines-in-a-textfile">c# how do I count lines in a textfile</a> <br>
  <a href="https://stackoverflow.com/questions/119559/determine-the-number-of-lines-within-a-text-file">Determine the number of lines within a text file</a> <br>
  <a href="https://stackoverflow.com/questions/6101367/how-to-count-lines-fast">How to count lines fast?</a></p>
  
  <p>So, I went ahead and ended up using what seems to be the most efficient (at least memory-wise?) method that I could find:</p>
  
  <pre><code>private static int countFileLines(string filePath)
  {
      using (StreamReader r = new StreamReader(filePath))
      {
          int i = 0;
          while (r.ReadLine() != null) 
          { 
              i++; 
          }
          return i;
      }
  }
  </code></pre>
  
  <p>But this takes forever when the lines themselves from the file are very long. Is there really not a faster solution to this?</p>
  
  <p>I've been trying to use <code>StreamReader.Read()</code> or <code>StreamReader.Peek()</code> but I can't (or don't know how to) make the either of them move on to the next line as soon as there's 'stuff' (chars? text?).</p>
  
  <p>Any ideas please?</p>
  
  <hr>
  
  <p><strong>CONCLUSION/RESULTS</strong> (After running some tests based on the answers provided):</p>
  
  <p>I tested the 5 methods below on two different files and I got consistent results that seem to indicate that plain old <code>StreamReader.ReadLine()</code> is still one of the fastest ways... To be honest, I'm perplexed after all the comments and discussion in the answers.</p>
  
  <p><strong>File #1:</strong> <br>
  Size: 3,631 KB <br>
  Lines: 56,870</p>
  
  <p>Results in seconds for File #1: <br>
  0.02 --> ReadLine method. <br>
  0.04 --> Read method. <br>
  0.29 --> ReadByte method. <br>
  0.25 --> Readlines.Count method. <br>
  0.04 --> ReadWithBufferSize method. <br></p>
  
  <p><strong>File #2:</strong> <br>
  Size: 14,499 KB <br>
  Lines: 213,424</p>
  
  <p>Results in seconds for File #1: <br>
  0.08 --> ReadLine method. <br>
  0.19 --> Read method. <br>
  1.15 --> ReadByte method. <br>
  1.02 --> Readlines.Count method. <br>
  0.08 --> ReadWithBufferSize method. <br></p>
  
  <p>Here are the 5 methods I tested based on all the feedback I received:</p>
  
  <pre><code>private static int countWithReadLine(string filePath)
  {
      using (StreamReader r = new StreamReader(filePath))
      {
      int i = 0;
      while (r.ReadLine() != null)
      {
          i++;
      }
      return i;
      }
  }
  
  private static int countWithRead(string filePath)
  {
      using (StreamReader _reader = new StreamReader(filePath))
      {
      int c = 0, count = 0;
      while ((c = _reader.Read()) != -1)
      {
          if (c == 10)
          {
          count++;
          }
      }
      return count;
      }            
  }
  
  private static int countWithReadByte(string filePath)
  {
      using (Stream s = new FileStream(filePath, FileMode.Open))
      {
      int i = 0;
      int b;
  
      b = s.ReadByte();
      while (b &gt;= 0)
      {
          if (b == 10)
          {
          i++;
          }
          b = s.ReadByte();
      }
      return i;
      }
  }
  
  private static int countWithReadLinesCount(string filePath)
  {
      return File.ReadLines(filePath).Count();
  }
  
  private static int countWithReadAndBufferSize(string filePath)
  {
      int bufferSize = 512;
  
      using (Stream s = new FileStream(filePath, FileMode.Open))
      {
      int i = 0;
      byte[] b = new byte[bufferSize];
      int n = 0;
  
      n = s.Read(b, 0, bufferSize);
      while (n &gt; 0)
      {
          i += countByteLines(b, n);
          n = s.Read(b, 0, bufferSize);
      }
      return i;
      }
  }
  
  private static int countByteLines(byte[] b, int n)
  {
      int i = 0;
      for (int j = 0; j &lt; n; j++)
      {
      if (b[j] == 10)
      {
          i++;
      }
      }
  
      return i;
  }
  </code></pre>