title: Java 8 parallel stream reduction - thread count vs speedup
body: <p>I am running an experiment on varying thread count for java 8 parallel stream's reduction operation: summation over 11.5 million numbers. I run it on 8 core intel xeon processor. I do not see much variation in the total execution time and total CPU time, when I vary the number of threads from 16 to 1. I <strong>do get speed-up</strong> over sequential version which uses 'streams' instead of `parallel streams'. I want to understand the correlation between no of threads and the speed-up over sequential reduction.</p>
  
  <p>Can somebody help me with the reasoning for this please? Is my code incorrect somewhere?</p>
  
  <p>Source code is <a href="https://github.com/yogirjoshi/monitortools/blob/master/src/main/java/rithm/driver/Hypothesis2.java" rel="nofollow">here</a>, line 74 performs parallel reduction.</p>
  
  <p>Relevant part of the code is pasted below as well:</p>
  
  <pre><code>class RedOperator implements BinaryOperator&lt;MonResult&gt;{
  
      @Override
      public MonResult apply(MonResult t, MonResult u) {
          // TODO Auto-generated method stub
          if(t != null &amp;&amp; u != null)
              t.count = t.count + u.count;
          return t;
      }
  
  }
  
  class FOFinisher implements Function&lt;ConcurrentHashMap&lt;ArrayList&lt;String&gt;, ArrayList&lt;MonResult&gt;&gt;, ArrayList&lt;MonResult&gt;&gt;{
  
  
      protected MonResult id;
      protected boolean isParallel;
      public FOFinisher(boolean isparallel){
          id = new MonResult();
          id.count = 0;
          this.isParallel = isparallel;
      }
      public long getJVMCpuTime() {
          long lastProcessCpuTime = 0;
          try {
              if (ManagementFactory.getOperatingSystemMXBean() instanceof OperatingSystemMXBean) {
                  lastProcessCpuTime=((com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean()).getProcessCpuTime();
              }
          }
          catch (  ClassCastException e) {
              System.out.println(e.getMessage());
          }finally{
              return lastProcessCpuTime;
          }
      }
      @Override
      public ArrayList&lt;MonResult&gt; apply(ConcurrentHashMap&lt;ArrayList&lt;String&gt;, ArrayList&lt;MonResult&gt;&gt; t) {
          // TODO Auto-generated method stub
  
          long beg = System.nanoTime();
          long begCPU = getJVMCpuTime();
          ArrayList&lt;MonResult&gt; ret;
          RedOperator x = new RedOperator();
          if(isParallel){
              ret = new ArrayList&lt;MonResult&gt;(t.values().stream().map((alist)-&gt;{
                          return alist.parallelStream().reduce(id,x);
                      }).collect(Collectors.toList()));
          }
          else{
              ret = new ArrayList&lt;MonResult&gt;(t.values().stream().map((alist)-&gt;{
                          return alist.stream().reduce(id,x);
                      }).collect(Collectors.toList()));
          }
  
          long end = System.nanoTime();
          long endCPU = getJVMCpuTime();
          System.out.println("Exec Time:" + TimeUnit.MILLISECONDS.convert((end-beg),TimeUnit.NANOSECONDS));
          System.out.println("CPU Time : " + TimeUnit.MILLISECONDS.convert((endCPU-begCPU),TimeUnit.NANOSECONDS));
          return ret;
      }
  
  }
  </code></pre>