Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The times you are reporting are measured using the clock function:</p> <blockquote> <p>The <a href="http://linux.die.net/man/3/clock" rel="noreferrer"><code>clock()</code></a> function returns an approximation of processor time used by the program. </p> </blockquote> <pre><code>$ time bin/amit_kumar_threads.cpp 6.62seconds serial: 2.7seconds real 0m5.247s user 0m9.025s sys 0m0.304s </code></pre> <p>The real time will be less for multiprocessor tasks, but the processor time will typically be greater. </p> <p>When you use multiple threads, the work may be done by more than one processor, but the amount of work is the same, and in addition there may be some overhead such as contention for limited resources. <code>clock()</code> measures the total processor time, which will be the work + any contention overhead. So it should never be less than the processor time for doing the work in a single thread. </p> <p>It's a little hard to tell from the question whether you knew this, and were surprised that the value returned by <code>clock()</code> was twice that for a single thread rather than being only a little more, or you were expecting it to be less.</p> <p>Using <a href="http://linux.die.net/man/3/clock_gettime" rel="noreferrer"><code>clock_gettime()</code></a> instead (you'll need the realtime library librt, <code>g++ -lrt</code> etc.) gives:</p> <pre><code>$ time bin/amit_kumar_threads.cpp 2.524 seconds serial: 2.761 seconds real 0m5.326s user 0m9.057s sys 0m0.344s </code></pre> <p>which still is less of a speed-up than one might hope for, but at least the numbers make some sense.</p> <p>100000000*20/2.5s = 800Hz, the bus frequency is 1600 MHz, so I suspect with a read and a write for each iteration (assuming some caching), you're memory bandwidth limited as tstenner suggests, and the <code>clock()</code> value shows that most of the time some of your processors are waiting for data. (does anyone know whether <code>clock()</code> time includes such stalls?)</p>
 

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