Sagar Panda
Sagar Panda

Reputation: 561

Java compilation optimization and performance

 public static void main(String[] args) {
     double [] boxes;
        boxes = new double[]  {20, 10, 5, 40, 20, 41, 41, 2, 6, 7, 3, 4, 5, 6, 23, 34, 7, 8, 2, 2};
        double heaviest = 0;
        double normal = 0;
        double heavy = 0;
        double totalCost;
        double a = 0;
        double b = 0;
        int repeatCount=0;

        for (int i = 1; i < boxes.length; i++) {
            if (boxes[i] > heaviest)
                heaviest = boxes[i];
        }

        for(double element: boxes) {
            if(element==heaviest) {
                repeatCount = repeatCount+1;
            }
        }

        System.out.println("Count :" +repeatCount);

        for (int j =0; j<boxes.length; j++) {
            if (boxes[j] < heaviest) {
                a = boxes[j] * 2;
                normal = normal+a;
            } else {
                b =  (boxes[j] * 3.5);
                heavy = heavy+b;
            }
        }
        totalCost = normal+heavy;
        System.out.println("total cost of the insuranse is  "+ totalCost);
    }

Part 1: I need to multiply the largest element by 3.5 and rest with 2 and then add the value to get the total.

Part 2: Also I want the number of occurrences of the largest element. OR We can also store the largest element in another array.

In my code, I'm done with the part 1 and for part 2, I'm taking the count of the occurrences of the largest element.

My question:

Is there any other way to do this for reducing the compilation time or for the code to be more optimized if there are more than 1000 elements?

I have also tried using the Collections.frequency(myArray, largestElement) by converting array to list.

Upvotes: 2

Views: 95

Answers (2)

Emz
Emz

Reputation: 1280

public static void main(String[] args) {
    double [] boxes = new double[]  {20, 10, 5, 40, 20, 41, 41, 2, 6, 7, 3, 4, 5, 6, 23, 34, 7, 8, 2, 2};
    double heaviest = 0;
    double normal = 0;
    double heavy = 0;
    double totalCost;
    int repeatCount=0;

    for (double d : boxes) {
        heaviest = Math.max (boxes[i], heaviest);
    }

    for (double d : boxes) {
        if(d == heaviest) {
            repeatCount++;
        }
    }
    System.out.println("Count: " + repeatCount);

    for (double d : boxes) {
        if (d == heaviest) {
            heavy += d * 2.0;       
        } else {
            heavy += d * 3.5;
        }
    }
    totalCost = normal+heavy;

    System.out.println("Total cost of the insuranse is: " + totalCost);
}

This is following your code, with your knowledge but fixed a bit. You never need to use i, nor do you need to store the results in a or b, instead you can user the += operator.

That being said double d1 == double d2 is dangerous to use. I do not recommend to use them directly. Take a read at this article or this. Actually, I recommend you to read both.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533540

As always, correctness is more important than performance.

Not sure why it ignores the first box when looking for the heaviest.

You could reduce the code, but your biggest problem is your code won't be run long enough to get compiled by the JIT. And even then it will be tiny compared to the startup cost of Java.

In short, I wouldn't worry about performance unless your program lasts for a few hundred milli-seconds.

BTW You could use a single loop which would make the code shorter, but like I said it won't make much difference in this case.

This is how I might write it.

double[] boxes = {20, 10, 5, 40, 20, 41, 41, 2, 6, 7, 3, 4, 5, 6, 23, 34, 7, 8, 2, 2};
double heaviest = -1;
int count = 0;
double sum = 0;
for (double box : boxes) {
    sum += box;
    if (box > heaviest) {
        count = 1;
        heaviest = box;
    } else if (box == heaviest) {
        count++;
    }
}

// double total = sum * 2 - heaviest * count * 2 + heaviest * count * 3.5;
double total = sum * 2 + heaviest * count * 1.5;
System.out.println("total: " + total);

Note: there is just one loop. You can work out the count and the sum as you go.

Upvotes: 6

Related Questions