title: Methods and their comparison for reading huge log file in perl
body: <p>I have a huge log file (around 5-10 milion lines). I need to traverse each and every line and do processing. I am seeing huge runtime.</p>
  
  <p>I am aware of two methods of reading files in <code>perl</code> as depicted below.</p>
  
  <p><strong>(1)</strong> How do I compare performance of different methods? Any bench-marking mechanism? </p>
  
  <p><strong>(2)</strong> What's the most efficient way and why? Is there any <em>third</em> and better method available?</p>
  
  <p>One option is to implement both methods and check for run-time. But here I trying understand what makes them run faster or slower. It would be appreciable if you could help me in this regards.</p>
  
  <p><strong>Method 1</strong></p>
  
  <pre><code>open FOPEN, '&lt;', $file or die $!;
  my @lines = &lt;FOPEN&gt;;
  chomp @lines;
  foreach (@lines) {
      # do something on $_
  }
  </code></pre>
  
  <p><strong>Method 2</strong></p>
  
  <pre><code>open FOPEN, '&lt;', $file or die $!;
  while (&lt;FOPEN&gt;) {
      chomp;
      # do something on $_
  }
  </code></pre>