loop starts from 0 is faster than loop starts from 1?
<p>look at these 2 loops</p>

<pre><code> const int arrayLength = ...
</code></pre>

<p>Version 0</p>

<pre><code>    public void RunTestFrom0()
    {
        int sum = 0;
        for (int i = 0; i &lt; arrayLength; i++)
            for (int j = 0; j &lt; arrayLength; j++)
                for (int k = 0; k &lt; arrayLength; k++)
                    for (int l = 0; l &lt; arrayLength; l++)
                        for (int m = 0; m &lt; arrayLength; m++)
                        {
                            sum += myArray[i][j][k][l][m];
                        }
    }
</code></pre>

<p>Version 1</p>

<pre><code>    public void RunTestFrom1()
    {
        int sum = 0;
        for (int i = 1; i &lt; arrayLength; i++)
            for (int j = 1; j &lt; arrayLength; j++)
                for (int k = 1; k &lt; arrayLength; k++)
                    for (int l = 1; l &lt; arrayLength; l++)
                        for (int m = 1; m &lt; arrayLength; m++)
                        {
                            sum += myArray[i][j][k][l][m];
                        }
    }
</code></pre>

<p>Version 2</p>

<pre><code>    public void RunTestFrom2()
    {
        int sum = 0;
        for (int i = 2; i &lt; arrayLength; i++)
            for (int j = 2; j &lt; arrayLength; j++)
                for (int k = 2; k &lt; arrayLength; k++)
                    for (int l = 2; l &lt; arrayLength; l++)
                        for (int m = 2; m &lt; arrayLength; m++)
                        {
                            sum += myArray[i][j][k][l][m];
                        }
    }
</code></pre>

<p>Results for <code>arrayLength=50</code> are (average from multiple sampling compiled X64):</p>

<ul>
<li>Version 0: 0.998s (Standard error of the mean 0.001s) total loops: 312500000</li>
<li>Version 1: 1.449s (Standard error of the mean 0.000s) total loops: 282475249</li>
<li>Version 2: 0.774s (Standard error of the mean 0.006s) total loops: 254803968</li>
<li>Version 3: 1.183s (Standard error of the mean 0.001s) total loops: 229345007</li>
</ul>

<p>if we make <code>arrayLength=45</code> then</p>

<ul>
<li>Version 0: 0.495s (Standard error of the mean 0.003s) total loops: 184528125</li>
<li>Version 1: 0.527s (Standard error of the mean 0.001s) total loops: 164916224</li>
<li>Version 2: 0.752s (Standard error of the mean 0.001s) total loops: 147008443</li>
<li>Version 3: 0.356s (Standard error of the mean 0.000s) total loops: 130691232</li>
</ul>

<p>why:</p>

<ol>
<li>loop start from 0 is faster than loop start from 1 though more loops</li>
<li>why loop start from 2 behaves weird?</li>
</ol>

<h3>Update:</h3>

<ul>
<li>I did each run 10 times, (that's where standard error of the mean comes from)</li>
<li>I also switched the order of version tests a couple of time. No big difference.</li>
<li>The length of <code>myArray</code> of each dimension = <code>arrayLength</code>, I initialized it in the beginning and the time taken is excluded. The value is 1. So <code>sum</code> gives the total loops.</li>
<li>The complied version is Released mode, and I run it from Outside VS. (Closed VS)</li>
</ul>

<h3>Update2:</h3>

<p>Now I discard <code>myArray</code> completely, <code>sum++</code> instead, and added <code>GC.Collect()</code></p>

<p><img src="http://i.stack.imgur.com/0i5rw.png" alt="enter image description here"></p>

<pre><code>    public void RunTestConstStartConstEnd()
    {
        int sum = 0;
        for (int i = constStart; i &lt; constEnd; i++)
            for (int j = constStart; j &lt; constEnd; j++)
                for (int k = constStart; k &lt; constEnd; k++)
                    for (int l = constStart; l &lt; constEnd; l++)
                        for (int m = constStart; m &lt; constEnd; m++)
                        {
                            sum++;
                        }
    }
</code></pre>
