Good,
I'm doing a job where I have to do tests on stacks, and the tests have to be done when I push and pop powers of 2 and powers of 2 plus one (1,2,3,4,5, 8,9,16,17, etc.) I have some code done:
public static void testsForLinkedLists(int maxPowerOf,
int intendedRepeats, String excelFile,
IntToDoubleFunction pushMethod, IntToDoubleFunction popMethod) {
ArrayList<Double> listOfPushTimes = new ArrayList<Double>();
ArrayList<Double> listOfPopTimes = new ArrayList<Double>();
double maxPush = 0;
double minPush = 100;
double maxPop = 0;
double minPop = 100;
double medianPush = 0;
double medianPop = 0;
double sumPush = 0;
double sumPop = 0;
double averagePush = 0;
double averagePop = 0;
int total = 0;
int excelLine = 1;
//int actualPowerOf = 0;
for (int actualPowerOf = 0; actualPowerOf < maxPowerOf; actualPowerOf++) {
listOfPushTimes.clear();
listOfPopTimes.clear();
//actualPowerOf++;
maxPush = 0;
minPush = 100;
maxPop = 0;
minPop = 100;
medianPush = 0;
medianPop = 0;
sumPush = 0;
sumPop = 0;
averagePush = 0;
averagePop = 0;
total = 0;
int powerOf2 = (int)Math.pow(2, actualPowerOf);
for (int i = 0; i < intendedRepeats; i++) {
double timetopush = pushMethod.applyAsDouble(powerOf2);
listOfPushTimes.add(timetopush);
sumPush = sumPush + timetopush;
if (timetopush > maxPush) {
maxPush = timetopush;
}
if (timetopush < minPush) {
minPush = timetopush;
}
double timetopop = pushMethod.applyAsDouble(powerOf2);
listOfPopTimes.add(timetopop);
sumPop = sumPop + timetopop;
if (timetopop > maxPop) {
maxPop = timetopop;
}
if (timetopop < minPop) {
minPop = timetopop;
}
total++;
}
averagePush = sumPush / total;
averagePop = sumPop / total;
medianPush = measuring_tools.medianOf(listOfPushTimes);
medianPop = measuring_tools.medianOf(listOfPopTimes);
}
My goal is to calculate the first power and to make tests, to load the next power and to make the respective tests ... until arriving at the maxPowerOf that the user introduced. If the user sets 4, tests will be done until (2 ^ 4) +1, ie 1,2,3,4,5,8,9,16,17
The problem is that in this way, I get two numbers two (2 ^ 0 + 1 and 2 ^ 1) and I still have to repeat lots of code to do the tests that are the same for both powers of two as for power of two plus one. Ideally, to have a cycle with the tests inside where the cycle was changing the value to the one desired by me (powers of two and two plus one). The ideal would perhaps be to have a cycle within another cycle.
Is there any way to optimize this code so that you do not repeat the number 2 and not have to repeat the whole test code?