Using a few different methods (binary search, nested loops etc.), I'm measuring the running time on average of finding common elements between two arrays. However, when I increase the # of time it loops (I divide runtime by number of loops at the end), the average runtime per iteration decreases. That is, it gets significantly faster.
I discovered the line in my main method that causes this, so I won't post the rest of main:
int intersection=studentList.intersectionSizeNestedLoops(firstList1,secondList1);
I looked at the runtime for each iteration instead of just the average at the end, and I noticed why exactly. For some reason, the first few iterations always take much longer (depends on list size, but e.g. 500 000 ns for the first few vs 20 000 ns for everything after, so it averages out to a lower run time when you have more iterations and thus more of the 20 000 ns iterations).
There's no way it's any of the algorithms though, so it must be the studentlist method.
Generates lists:
public studentList(int size, String course) {
int IDrange = 2*size;
studentID = new int[size];
boolean[] usedID = new boolean[IDrange];
for (int i = 0; i < IDrange; i++)
usedID[i] = false;
for (int i=0;i<size;i++) {
int t;
do {
t = (int) (Math.random()*IDrange);
} while (usedID[t]);
usedID[t] = true;
studentID[i] = t;
}
courseName = course;
numberOfStudents = size;
}
Thank you.