title: Why is Files.list() parallel stream performing so much slower than using Collection.parallelStream()?
body: <p>The following code fragment is part of a method that gets a directory listing, calls an extract method on each file and serializes the resulting drug object to xml.</p>
  
  <pre><code>try(Stream&lt;Path&gt; paths = Files.list(infoDir)) {
      paths
          .parallel()
          .map(this::extract)
          .forEachOrdered(drug -&gt; {
              try {
                  marshaller.write(drug);
              } catch (JAXBException ex) {
                  ex.printStackTrace();
              }
          });
  }
  </code></pre>
  
  <p>Here is the exact same code doing the exact same thing but using a plain <code>.list()</code> call to get the directory listing and calling <code>.parallelStream()</code> on the resulting list.</p>
  
  <pre><code>Arrays.asList(infoDir.toFile().list())
      .parallelStream()
      .map(f -&gt; infoDir.resolve(f))
      .map(this::extract)
      .forEachOrdered(drug -&gt; {
          try {
              marshaller.write(drug);
          } catch (JAXBException ex) {
              ex.printStackTrace();
      }
  });
  </code></pre>
  
  <p>My machine is a quad core MacBook Pro, Java v 1.8.0_60 (build 1.8.0_60-b27).</p>
  
  <p>I am processing ~ 7000 files. The averages of 3 runs:</p>
  
  <p>First version:
  With <code>.parallel()</code>:  20 seconds. Without <code>.parallel()</code>: 41 seconds</p>
  
  <p>Second version:
  With <code>.parallelStream()</code>: 12 seconds. With <code>.stream()</code>: 41 seconds.</p>
  
  <p>Those 8 seconds in parallel mode seem like an enormous difference given that the <code>extract</code> method that reads from the stream and does all the heavy work and the <code>write</code> call doing the final writes are unchanged. </p>