title: How to count lines fast?
body: <p>I tried <a href="http://unxutils.sourceforge.net/" rel="noreferrer">unxutils</a>' <code>wc -l</code> but it crashed for 1GB files. I tried this C# code</p>
  <pre><code>long count = 0;
  using (StreamReader r = new StreamReader(f))
  {
      string line;
      while ((line = r.ReadLine()) != null)
      {
          count++;
      }
  }
  
  return count;
  </code></pre>
  <p>It reads a 500MB file in 4 seconds</p>
  <pre><code>var size = 256;
  var bytes = new byte[size];
  var count = 0;
  byte query = Convert.ToByte('\n');
  using (var stream = File.OpenRead(file))
  {
      int many;
      do
      {
          many = stream.Read(bytes, 0, size);
          count += bytes.Where(a =&gt; a == query).Count();                    
      } while (many == size);
  }
  </code></pre>
  <p>Reads in 10 seconds</p>
  <pre><code>var count = 0;
  int query = (int)Convert.ToByte('\n');
  using (var stream = File.OpenRead(file))
  {
      int current;
      do
      {
          current = stream.ReadByte();
          if (current == query)
          {
              count++;
              continue;
          }
      } while (current!= -1);
  }
  </code></pre>
  <p>Takes 7 seconds</p>
  <p>Is anything faster I haven't tried yet?</p>