title: Why is creating an array with inline initialization so slow?
body: <p>Why is inline array initialization so much slower than doing so iteratively? I ran this program to compare them and the single initialization takes many times longer than doing so with a <code>for</code> loop. </p>
  
  <p>Here's the program I wrote in <code>LinqPad</code> to test this.</p>
  
  <pre><code>var iterations = 100000000;
  var length = 4;
  
  {
      var timer = System.Diagnostics.Stopwatch.StartNew();
  
      for(int i = 0; i &lt; iterations; i++){
          var arr = new int[] { 1, 2, 3, 4 };
      }
      timer.Stop();
      "Array- Single Init".Dump();
      timer.Elapsed.Dump();
  }
  
  {
      var timer = System.Diagnostics.Stopwatch.StartNew();
  
      for(int i = 0; i &lt; iterations; i++){
          var arr = new int[length];
          for(int j = 0; j &lt; length; j++){
              arr[j] = j;
          }
      }
      timer.Stop();
      "Array- Iterative".Dump();
      timer.Elapsed.Dump();
  }
  </code></pre>
  
  <p>Results:</p>
  
  <pre><code>Array - Single Init
  00:00:26.9590931
  
  Array - Iterative
  00:00:02.0345341
  </code></pre>
  
  <p>I also ran this on <strong>VS2013 Community Edition</strong> and the <strong>latest VS2015 preview</strong> on a different PC and got similar results to my <code>LinqPad</code> results. </p>
  
  <p>I ran the code in <code>Release</code> mode (i.e.: compiler optimizations on), and got very different results from above. The two code blocks came out very similar this time. This seems to indicate it is a compiler optimization issue.</p>
  
  <pre><code>Array - Single Init
  00:00:00.5511516
  
  Array - Iterative
  00:00:00.5882975
  </code></pre>