Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti threaded object creation slower then in a single thread
    primarykey
    data
    text
    <p>I have what probably is a basic question. When I create 100 million Hashtables it takes approximately 6 seconds (runtime = 6 seconds per core) on my machine if I do it on a single core. If I do this multi-threaded on 12 cores (my machine has 6 cores that allow hyperthreading) it takes around 10 seconds (runtime = 112 seconds per core).</p> <p>This is the code I use:</p> <p><strong>Main</strong></p> <pre><code>public class Tests { public static void main(String args[]) { double start = System.currentTimeMillis(); int nThreads = 12; double[] runTime = new double[nThreads]; TestsThread[] threads = new TestsThread[nThreads]; int totalJob = 100000000; int jobsize = totalJob/nThreads; for(int i = 0; i &lt; threads.length; i++) { threads[i] = new TestsThread(jobsize,runTime, i); threads[i].start(); } waitThreads(threads); for(int i = 0; i &lt; runTime.length; i++) { System.out.println("Runtime thread:" + i + " = " + (runTime[i]/1000000) + "ms"); } double end = System.currentTimeMillis(); System.out.println("Total runtime = " + (end-start) + " ms"); } private static void waitThreads(TestsThread[] threads) { for(int i = 0; i &lt; threads.length; i++) { while(threads[i].finished == false)//keep waiting untill the thread is done { //System.out.println("waiting on thread:" + i); try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } } } </code></pre> <p><strong>Thread</strong></p> <pre><code>import java.util.HashMap; import java.util.Map; public class TestsThread extends Thread { int jobSize = 0; double[] runTime; boolean finished; int threadNumber; TestsThread(int job, double[] runTime, int threadNumber) { this.finished = false; this.jobSize = job; this.runTime = runTime; this.threadNumber = threadNumber; } public void run() { double start = System.nanoTime(); for(int l = 0; l &lt; jobSize ; l++) { double[] test = new double[65]; } double end = System.nanoTime(); double difference = end-start; runTime[threadNumber] += difference; this.finished = true; } } </code></pre> <p>I do not understand why creating the object simultaneously in multiple threads takes longer per thread then doing it in serial in only 1 thread. If I remove the line where I create the Hashtable this problem disappears. If anyone could help me with this I would be greatly thankful.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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