title: Why are iterators in Rust seemingly very slow when I iterate over them?
body: <p>I have a large file that I am reading from using mmap. I want to do some operations on each line, so I call split() on it, which gives me an iterator for each line:</p>
  <pre><code>let file = File::open(&quot;myfile&quot;).unwrap();
  let mmap = unsafe { MmapOptions::new().map(&amp;file).unwrap() };
  //splitting by newline
  let iter = mmap.split(|elem| elem == &amp;b'\n');
  </code></pre>
  <p>This works fine and doesn't give me any issue - runs very fast.</p>
  <p>However, when I go through the iterator, it jumps and the time it takes to go through the
  for loop is around 4 times the time it took to read and split.</p>
  <p>Also, this is without processing the line or doing anything inside the for loop:</p>
  <pre><code>for elem in iter {
    //process the line
  }
  </code></pre>
  <p>Since performance is an issue - I find it odd that it manages to read &amp; split the file very fast, however, it's becoming really slow when I go through the iterator. Am I missing something? My knowledge of rust is also limited so not sure if I am doing something bad. Is there something that can help me optimize this and get me faster access times?</p>
  <p>Also, parallel iterators are not that helpful in my case - the overhead that they add is not worth.</p>
  <p>Whole file:</p>
  <pre><code>use memmap::MmapOptions;
  use std::fs::File;
  use std::time::{Duration, Instant};
  
  fn main() {
  
      let now = Instant::now();
      let file = File::open(&quot;myfile&quot;).unwrap();
      let mmap = unsafe { MmapOptions::new().map(&amp;file).unwrap() };
      let iter = mmap.split(|elem| elem == &amp;b'\n');
  
      /*
      for elem in iter {
        //do nothing
      }
      */
      println!(&quot;{:?}&quot;, now.elapsed());
  }
  </code></pre>
  <p>If I uncomment the for loop, it becomes 4 times slower. I am building with --release tag so that is not an issue.</p>