title: Why is Kotlin's map-filter-reduce slower than Java's Stream operations on large inputs?
body: <p>A few days ago I created a simple benchmark (without jmh and all of the another specialized stuff, just to measure roughly).</p>
  
  <p>I've found that for the same simple task (iterate through 10 million numbers, square them, filter only even numbers and reduce their sum), Java works much faster. Here's the code:</p>
  
  <p>Kotlin:</p>
  
  <pre><code>fun test() {
      println((0 .. 10_000_000L).map { it * it }
                                .filter { it % 2 == 0L }
                                .reduce { sum, it -&gt; sum + it })
  }
  </code></pre>
  
  <p>Java:</p>
  
  <pre><code>public void test() {
      System.out.println(LongStream.range(0, 10_000_000)
                                   .map(it -&gt; it * it)
                                   .filter(it -&gt; it % 2 == 0)
                                   .reduce((sum, it) -&gt; sum + it)
                                   .getAsLong());
  }
  </code></pre>
  
  <p>I'm using Java version 1.8.0_144 and Kotlin version 1.2.</p>
  
  <p>On my hardware in average it takes <strong>85</strong>ms for Java and <strong>4,470</strong>ms for Kotlin to execute the corresponding functions. <strong>Kotlin works 52 times slower.</strong></p>
  
  <p>I suspect that the Java compiler produces optimized bytecode, but I didn't expected to see such a huge difference. I'm wondering if I'm doing something wrong? How can I compel Kotlin to work faster? I like it because of its syntax, but 52 times is a big difference. And I just wrote Java 8-like code, not the plain old iterative version (which, I believe, will be much faster than given one).</p>