title: Why does "#pragma omp simd" only take big performance improvement in "-O2" under gcc compiler?
body: <p>Check the following code:  </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;omp.h&gt;
  
  #define ARRAY_SIZE  (1024)
  float A[ARRAY_SIZE];
  float B[ARRAY_SIZE];
  float C[ARRAY_SIZE];
  
  int main(void)
  {   
      for (int i = 0; i &lt; ARRAY_SIZE; i++)
      {
          A[i] = i * 2.3;
          B[i] = i + 4.6;
      }
  
      double start = omp_get_wtime();
      for (int loop = 0; loop &lt; 1000000; loop++)
      {
          #pragma omp simd
          for (int i = 0; i &lt; ARRAY_SIZE; i++)
          {
              C[i] = A[i] * B[i];
          }
      }
      double end = omp_get_wtime();
      printf("Work consumed %f seconds\n", end - start);
      return 0;
  }
  </code></pre>
  
  <p>Build and run it on my machine, it outputs:  </p>
  
  <pre><code>$ gcc -fopenmp parallel.c
  $ ./a.out
  Work consumed 2.084107 seconds
  </code></pre>
  
  <p>If I comment out "<code>#pragma omp simd</code>", build and run it again:  </p>
  
  <pre><code>$ gcc -fopenmp parallel.c
  $ ./a.out
  Work consumed 2.112724 seconds
  </code></pre>
  
  <p>We can see "<code>#pragma omp simd</code>" doesn't get big performance gain. But if I add <code>-O2</code> option, no "<code>#pragma omp simd</code>":  </p>
  
  <pre><code>$ gcc -O2 -fopenmp parallel.c
  $ ./a.out
  Work consumed 0.446662 seconds
  </code></pre>
  
  <p>With "<code>#pragma omp simd</code>":</p>
  
  <pre><code>$ gcc -O2 -fopenmp parallel.c
  $ ./a.out
  Work consumed 0.126799 seconds
  </code></pre>
  
  <p>We can see a big improvement. But if use <code>-O3</code>, no "<code>#pragma omp simd</code>": </p>
  
  <pre><code>$ gcc -O3 -fopenmp parallel.c
  $ ./a.out
  Work consumed 0.127563 seconds
  </code></pre>
  
  <p>with "<code>#pragma omp simd</code>":</p>
  
  <pre><code>$ gcc -O3 -fopenmp parallel.c
  $ ./a.out
  Work consumed 0.126727 seconds
  </code></pre>
  
  <p>We can see the results are similar again.  </p>
  
  <p>Why does "<code>#pragma omp simd</code>" only take big performance improvement in <code>-O2</code> under <code>gcc</code> compiler?</p>