Why do multiple iterations of a block of code increase average running time so much?
<p>For example, I'm running a simple nested loop that finds duplicate elements in two arrays.  With a list size of 2000, it takes somewhere around 25 milliseconds on average (a guess) if I run the program several times in a row.  However, running the program once within a for loop, the more times I loop it, the faster the average runtime for one iteration is.  e.g. 10 times is 5 milliseconds each iteration, 10000 times is 2 milliseconds.</p>

<p>I have to measure the running time for a few different types of list intersection (e.g. nested loops, binary search etc.) at various list sizes, so I'm not sure which is more accurate.  It becomes more of a problem for the longer ones because it takes too long to run it so many times.</p>

<pre><code>    for (int i = 0; i &lt; A.length-1; i++) {
        for (int j = 0; j &lt; B.length-1; j++) {
            if (A[i] == B[j]) {
                inter++;
            }
        }
    }
</code></pre>

<p>How I generate the lists used:</p>

<pre><code>public studentList(int size, String course) {
int IDrange=2*size;
studentID=new int[size];
boolean[] usedID=new boolean[IDrange];
for (int i=0;i&lt;IDrange;i++) usedID[i]=false;
for (int i=0;i&lt;size;i++) {
    int t;
    do {
    t=(int)(Math.random()*IDrange);
    } while (usedID[t]);
    usedID[t]=true;
    studentID[i]=t;
}
courseName=course;
numberOfStudents=size;
}
</code></pre>
