Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does GCD increase execution time?
    primarykey
    data
    text
    <p>I try to learn Grand Central Dispatch (GCD) and use the following code to test:</p> <p>With GCD:</p> <pre><code>#include &lt;dispatch/dispatch.h&gt; #include &lt;vector&gt; #include &lt;cstdlib&gt; #include &lt;iostream&gt; int main(int argc, char *argv[]) { const int N = atoi(argv[1]); __block std::vector&lt;int&gt; a(N, 0); dispatch_apply(N, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t i) { a[i] = i; #ifdef DEBUG if ( i % atoi(argv[2]) == 0) std::cout &lt;&lt; a[i] &lt;&lt; std::endl; #endif }); return 0; } </code></pre> <p>Without GCD:</p> <pre><code>#include &lt;vector&gt; #include &lt;cstdlib&gt; #include &lt;iostream&gt; int main(int argc, char *argv[]) { const int N = atoi(argv[1]); std::vector&lt;int&gt; a(N, 0); for (int i = 0; i &lt; N; i++) { a[i] = i; #ifdef DEBUG if ( i % atoi(argv[2]) == 0) std::cout &lt;&lt; a[i] &lt;&lt; std::endl; #endif } return 0; } </code></pre> <p>The test result with GCD:</p> <pre><code>$ time ./testgcd 100000000 10000000 4.254 secs </code></pre> <p>The test without GCD:</p> <pre><code>$ time ./nogcd 100000000 10000000 1.462 secs </code></pre> <p>I thought that GCD should reduce execution time, but the results show the opposite. I am not sure if I misuse GCD. The OS environment is Mac OS X 10.8 with Xcode 4.5. The compiler is Clang++ 3.1. The hardware is Macbook Pro with i5 CPU, which has two cores. </p> <p>For comparison, I use OpenMP (also using GCC shipped with Xcode 4.5 on the same laptop):</p> <pre><code>#include &lt;vector&gt; #include &lt;cstdlib&gt; int main(int argc, char *argv[]) { const int N = atoi(argv[1]); std::vector &lt;int&gt; a(N, 0); #pragma omp parallel for for (int i = 0; i &lt; N; i++) a[i] = i; return 0; } </code></pre> <p>and w/wo (-fopenmp), I have two executables to test, </p> <p>with <code>-fopenmp</code> flag while compiling:</p> <pre><code>$ time ./testopenmp 100000000 1.280 secs </code></pre> <p>without <code>-fopenmp</code> flag while compiling: </p> <pre><code>$ time ./testnoopenmp 100000000 1.626 secs </code></pre> <p>With OpenMP, the executing time are reduced. </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