Note that there are some explanatory texts on larger screens.

plurals
  1. POJava ForkJoin Multi-threaded is slower than single thread
    primarykey
    data
    text
    <p>I was trying out the Java ForkJoin framework and wrote a simple test program that sets the pixels of an image to random colors. E.g. it generates pseudo-noise. </p> <p>But while testing performance I found that it's actually faster to run single threaded than to run it with multiple threads. I make it run single threaded by passing a high threshold. </p> <p>This is the class worker class:</p> <pre><code>public class Noise extends RecursiveAction { private BufferedImage image; private int xMin; private int yMin; private int xMax; private int yMax; private int threshold = 2000000; // max pixels per thread public Noise(BufferedImage image, int xMin, int yMin, int xMax, int yMax, int threshold) { this.image = image; this.xMin = xMin; this.yMin = yMin; this.xMax = xMax; this.yMax = yMax; this.threshold = threshold; } public Noise(BufferedImage image, int xMin, int yMin, int xMax, int yMax) { this.image = image; this.xMin = xMin; this.yMin = yMin; this.xMax = xMax; this.yMax = yMax; } @Override protected void compute() { int ppt = (xMax - xMin) * (yMax - yMin); // pixels pet thread if(ppt &gt; threshold) { // split int verdeling = ((xMax - xMin) / 2) + xMin; invokeAll(new Noise(image, xMin, yMin, verdeling, yMax), new Noise(image, verdeling+1, yMin, xMax, yMax)); } else { // execute! computeDirectly(xMin, yMin, xMax, yMax); } } private void computeDirectly(int xMin, int yMin, int xMax, int yMax) { Random generator = new Random(); for (int x = xMin; x &lt; xMax; x++) { for (int y = yMin; y &lt; yMax; y++) { //image.setPaint(new Color(generator.nextInt())); int rgb = generator.nextInt(); int red = (rgb &gt;&gt; 16) &amp; 0xFF; int green = (rgb &gt;&gt; 8) &amp; 0xFF; int blue = rgb &amp; 0xFF; red = (int) Math.round((Math.log(255L) / Math.log((double) red)) * 255); green = (int) Math.round((Math.log(255L) / Math.log((double) green)) * 255); blue = (int) Math.round((Math.log(255L) / Math.log((double) blue)) * 255); int rgbSat = red; rgbSat = (rgbSat &lt;&lt; 8) + green; rgbSat = (rgbSat &lt;&lt; 8) + blue; image.setRGB(x, y, rgbSat); } } Graphics2D g2D = image.createGraphics(); g2D.setPaint(Color.RED); g2D.drawRect(xMin, yMin, xMax-xMin, yMax-yMin); } } </code></pre> <p>When generating a 6000 * 6000 image the results are:<br> Single thread: 9.4sec @ 25% CPU load<br> Multi thread: 16.5sec @ 80%-90% CPU load<br> (Core2quad Q9450)</p> <p>Why is the multi-threaded version slower?<br> How do I fix this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload