for (int i = 0; i < 19; i++) {
long t1 = System.nanoTime();
long total = 0;
for (int j = 0; j < 5000; j++) {
x.algorithm1(randomArray[i]);
long t2 = System.nanoTime();
long elapsed = t2 - t1;
total += elapsed;
}
long averageTime = 0;
averageTime= total / 5000;
matrix[i][0] = averageTime;
}
/* Running Algorithm 2 */
for (int i = 0; i < 19; i++) {
long t1 = System.nanoTime();
long total = 0;
for (int j = 0; j < 5000; j++) {
x.algorithm2(randomArray[i]);
long t2 = System.nanoTime();
long elapsed = t2 - t1;
total += elapsed;
}
long averageTime = 0;
averageTime = total / 5000;
matrix[i][1] = averageTime;
}
I have 3 of these for loops that basically do the same thing however the line "x.algorithm2(randomArray[i]);" changes to a different algorithm in each different for loop. How can I reduce this repetitive code to only change the algorithm being called?