How to optimize a power loop of 2 n and (2 n)+1 in java?

Asked

Viewed 49 times

0

Good,

I’m doing a job where I have to do tests in Stacks, and the tests have to be done when I push and pop of powers of 2 and powers of 2 plus one (1,2,3,4,5,8,9,16,17, etc...) I got some code made:

 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 do tests, load the next power and do the respective tests ... until you reach the maxPowerOf that the user introduced. If the User puts 4, tests will be made up to (2 4)+1, that is 1,2,3,4,5,8,9,16,17

The problem is that 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 equal for powers of two and power of two plus one. The ideal would be to have a cycle with the tests inside where the cycle was changing the value to what I wanted (powers of two and two plus one). The ideal perhaps would be to have a cycle within another cycle

Is there any way to optimize this code to not repeat the number 2 and not have to repeat the whole test code?

  • @diegofm it was I who asked this question and now I did this that is related but is different

  • I know, that’s why I connected.

  • It’s not just start the Indice for 1? for (int i = 1; i <= maxPowerOf; i++)

  • @diegofm Here comes the first result. Just do not do the second case when the exponent is zero, can answer this.

  • No, because that’s not 2 0=1

  • @jbueno sees my Dit, maybe it helps

  • @user6273698 No way I’m gonna stop to read this giant lump of code. Please try to be more succinct.

  • @jbueno edited, the only thing you need to see is the cycle for

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.