title: Why is a program that updates a large static floating point array slower in Rust than in C?
body: <p>I wrote a simple program to compare Rust and C performance.  </p>
  
  <p>The Rust version:</p>
  
  <pre class="lang-rust prettyprint-override"><code>use std::time::Instant;
  
  const STREAM_ARRAY_SIZE: usize = 10000000;
  static mut A: [f64; STREAM_ARRAY_SIZE] = [1.0; STREAM_ARRAY_SIZE];
  
  fn main() {
      let now = Instant::now();
  
      unsafe {
          for i in 0..STREAM_ARRAY_SIZE {
              A[i] = 2.0E0 * A[i];
          }
      }
  
      let duration = now.elapsed();
      println!("{}", (duration.as_secs() * 1_000_000_000 + duration.subsec_nanos() as u64) / 1000);
  }
  </code></pre>
  
  <p>Run it in debug &amp; release mode:  </p>
  
  <pre class="lang-none prettyprint-override"><code>$ ./target/debug/calc
  472046 us.
  $ ./target/release/calc
  62860 us.
  </code></pre>
  
  <p>The release version has a drastic performance gain compared to debug.  </p>
  
  <p>The C version does the same thing and runs on the same server:  </p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;sys/time.h&gt;
  #include &lt;stdio.h&gt;
  
  #define STREAM_ARRAY_SIZE   10000000
  
  static double A[STREAM_ARRAY_SIZE];
  int mysecond(void)
  {
          struct timeval tp;
          struct timezone tzp;
          int i;
  
          i = gettimeofday(&amp;tp,&amp;tzp);
          return (tp.tv_sec * 1000000 + tp.tv_usec);
  }
  
  int main(void)
  {
      int j = 0;
      for (j = 0; j &lt; STREAM_ARRAY_SIZE; j++)
      {
          A[j] = 1.0;
      }
  
      int t = mysecond();
      for (j = 0; j &lt; STREAM_ARRAY_SIZE; j++)
      {
          A[j] = 2.0E0 * A[j];
      }
      printf("%d us.\n", mysecond() - t);
      return 0;
  }
  </code></pre>
  
  <p>Compile and run it with <code>-O0</code> and <code>-O2</code>:  </p>
  
  <pre class="lang-none prettyprint-override"><code>$ gcc test.c
  $ ./a.out
  41626 us.
  $ gcc -O2 test.c
  $ ./a.out
  13499 us.
  </code></pre>
  
  <p>The Rust optimized version only compares to <code>gcc -O0</code>, and is very weak compared to <code>gcc -O2</code>. Is this reasonable? How can I improve the performance of the Rust version?</p>