title: OpenMP slower than single threaded even though embarrassingly parallelizable
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;omp.h&gt;
  #include &lt;math.h&gt;
  #include &lt;time.h&gt;
  
  
  void func1(float *x, int n) {
      for (int ind=0; ind&lt;n; ind++) {
          x[ind] = fabsf(x[ind]);
      }
  }
  
  void func2(float *x, int n) {
      #pragma omp parallel for
      for (int ind=0; ind&lt;n; ind++) {
          x[ind] = fabsf(x[ind]);
      }
  }
  
  
  int main(void) {
      int TIMES = 10;
      int N = 1880000*128;
      float *arr = (float*)malloc(N*sizeof(float));
  
      float start_time1 = (float)clock() / CLOCKS_PER_SEC;
      for (int t=0; t&lt;TIMES; t++) {
          func1(arr, N);
      }
      float duration1 = (float)clock() / CLOCKS_PER_SEC - start_time1;
  
      float start_time2 = (float)clock() / CLOCKS_PER_SEC;
      for (int t=0; t&lt;TIMES; t++) {
          func2(arr, N);
      }
      float duration2 = (float)clock() / CLOCKS_PER_SEC - start_time2;
  
      free(arr);
      printf("duration1: %.4f\n", duration1 / TIMES);
      printf("duration2: %.4f\n", duration2 / TIMES);
      return 0;
  }
  </code></pre>
  
  <p>I compiled with GCC (4.8.4) that came with Ubuntu 14.04:</p>
  
  <pre><code>&gt; gcc -O3 -std=c99 -fopenmp -o test test.c -lm
  &gt; ./test
  duration1: 0.1090
  duration2: 0.3777
  </code></pre>
  
  <p>Why is <code>func2</code> slower? I am using the c4.2xlarge instance in AWS (8CPU, 16GB mem).</p>