Note that there are some explanatory texts on larger screens.

plurals
  1. POMultithreading not faster than single thread (simple loop test)
    primarykey
    data
    text
    <p>I'm experimenting with some multithreading constructions, but somehow it seems that multithreading is not faster than a single thread. I narrowed it down to a very simple test with a nested loop (1000x1000) in which the system only counts.<br> Below I posted the code for both single threading and multithreading and how they are executed.<br> The result is that the single thread completes the loop in about <strong>110 ms</strong>, while the two threads also take about <strong>112 ms</strong>.<br> I don't think the problem is the overhead of multithreading. If I only submit one of both Runnables to the ThreadPoolExecutor, it executes in half the time of the single thread, which makes sense. But adding that second Runnable makes it 10 times slower. Both 3.00Ghz cores are running 100%.<br> I think it may be pc-specific, as someone else's pc showed double-speed results on the multithreading. But then, what can I do about it? I have a Intel Pentium 4 3.00GHz (2 CPUs) and Java jre6. <br><br> Test code:</p> <pre><code>// Single thread: long start = System.nanoTime(); // Start timer final int[] i = new int[1]; // This is to keep the test fair (see below) int i = 0; for(int x=0; x&lt;10000; x++) { for(int y=0; y&lt;10000; y++) { i++; // Just counting... } } int i0[0] = i; long end = System.nanoTime(); // Stop timer </code></pre> <p>This code is executed in about <strong>110 ms</strong>.</p> <pre><code>// Two threads: start = System.nanoTime(); // Start timer // Two of the same kind of variables to count with as in the single thread. final int[] i1 = new int [1]; final int[] i2 = new int [1]; // First partial task (0-5000) Thread t1 = new Thread() { @Override public void run() { int i = 0; for(int x=0; x&lt;5000; x++) for(int y=0; y&lt;10000; y++) i++; i1[0] = i; } }; // Second partial task (5000-10000) Thread t2 = new Thread() { @Override public void run() { int i = 0; for(int x=5000; x&lt;10000; x++) for(int y=0; y&lt;10000; y++) i++; int i2[0] = i; } }; // Start threads t1.start(); t2.start(); // Wait for completion try{ t1.join(); t2.join(); }catch(Exception e){ e.printStackTrace(); } end = System.nanoTime(); // Stop timer </code></pre> <p>This code is executed in about <strong>112 ms</strong>.</p> <p><strong>Edit: I changed the Runnables to Threads and got rid of the ExecutorService (for simplicity of the problem).</strong></p> <p><strong>Edit: tried some suggestions</strong></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