title: Why is the foreach lambda so much slower than the other for loops?
body: <p>I have lists that I need to iterate through. I was using the new foreach from Java 8. I found it to be much slower than older less elegant methods. I've read <a href="https://stackoverflow.com/questions/16635398/java-8-iterable-foreach-vs-foreach-loop">this</a> stack discussion which focuses more on <em>best practices</em> rather than real world implementation. I understand that with the lambda there is additional overhead that stems from method passing, but I still don't understand the discrepancy between the first and third case and that has me thinking I could be using it wrong. Are there not yet implemented optimizations for the lambda or is there a better way I could be using them to get more performance? Below is a demonstrative sample and not my actual code.  </p>
  
  <pre><code>import java.util.Arrays;
  import java.util.Iterator;
  import java.util.List;
  
  public class Main {
          public static void main(String[] args) {
  
              List&lt;Memes&gt; memesList =
                      Arrays.asList(
                              new OldMemes(),
                              new TrendingMemes()
                      );
  
              //----------------------------Benchmark
              long start, end;
              start = System.nanoTime();
              System.out.format("for each started @%d\n", start);
              //Old way.
              for(Memes memes : memesList){
                  memes.showMeme();
              }
              //-----------------------------Benchmark
              end = System.nanoTime();
              System.out.println((end - start));
              start = System.nanoTime();
              System.out.format("for started @%d\n", start);
              //Other way
              for(Iterator&lt;Memes&gt; i = memesList.iterator(); i.hasNext(); ) {
                  i.next().showMeme();
              }
              //-----------------------------Benchmark
              end = System.nanoTime();
              System.out.println((end - start));
              start = System.nanoTime();
              System.out.format("for each lambda started @%d\n", start);
  
              //New way.
              memesList.forEach((memes) -&gt; memes.showMeme());
  
              //-----------------------------Benchmark
              end = System.nanoTime();
              System.out.println((end - start));
              System.out.format("end @%d\n", end);
          }
      interface Memes {
          public void showMeme();
      }
  
      static class OldMemes implements Memes {
  
          @Override
          public void showMeme() {
              System.out.println("Id appetere senserit his, nonumes consulatu vel id.");
          }
  
      }
  
      static class TrendingMemes implements Memes {
  
          @Override
          public void showMeme() {
              System.out.println("Id appetere senserit his, nonumes consulatu vel id.");
          }
      }
  }
  </code></pre>