green-code-initiative / ecoCode-challenge

Emboard in the hackhatons serie for improving ecoCode
3 stars 4 forks source link

[Hackaton 2024][Rhodium-Antimoine][JAVA] Java (multiple If/Else/Elseif) #120

Open AntoineMeheut opened 1 month ago

AntoineMeheut commented 1 month ago

Associated rule title

[JAVA] Java (multiple If/Else/Elseif)

Associated rule link

https://github.com/green-code-initiative/ecoCode/blob/main/ecocode-rules-specifications/src/main/rules/EC2/java/EC2.asciidoc

Language and platform

Example: Java Itel proc 12th Gen Core i7-1255U 1,7 GHz
Docker container : python:3.9-slim
memory = 4go swap = 6go cpu=2

Measure justification

Profiling with timestamp 30 secondes minimum execution Calculating the differential between code implementing bad code and code implementing good code practice

Validation

The rule is validated by the measurement. 
The code used to validate: public class Main {

public static void main(String[] args) {
    new Main().runTest();
}

private void executeBad() {
    int index = 1;
    int nb = 2;
    if (nb == 0) {
        nb = index;
    } else if (nb == 1) {
        nb = index * 2;
    } else if (nb == 2) {
        nb = index * 3;
    } else {
        nb = -1;
    }
}

private void executeGood() {
    int index = 1;
    int nb = 2;
    switch (nb) {
        case 0:
            nb = index;
            break;
        case 1:
            nb = index * 2;
            break;
        case 2:
            nb = index * 3;
            break;
        default:
            nb = -1;
    }
}

public void runTest() {
    // Initialize
    long totalGood = 0;
    long totalBad = 0;
    long nb = 10;
    long nbIteration = 0;
    long beginTest = System.currentTimeMillis();

    // Run test
    do {
        // Test good
        long begin = System.currentTimeMillis();
        for(int i = 0; i < nb; i++) {
            executeGood();
        }
        long end = System.currentTimeMillis();
        totalGood += end - begin;

        // Test bad
        begin = System.currentTimeMillis();
        for(int i = 0; i < nb; i++) {
            executeBad();
        }
        end = System.currentTimeMillis();
        totalBad += end - begin;

        // Update counters
        nbIteration += nb;

        // minimum 30 seconds of test
    } while((System.currentTimeMillis() - beginTest) < 60_000);

    // Print results
    printResults(totalGood, totalBad, nbIteration);
}

private void printResults(long totalGood, long totalBad, long nbIteration) {
    int difference = (int) (((totalBad - totalGood) * 100) / totalGood);
    System.out.println("Total good: " + totalGood + " ms");
    System.out.println("Total bad: " + totalBad + " ms");
    System.out.println("Total iteration: " + nbIteration);
    System.out.println("\nAverage good: " + (totalGood / nbIteration) + " ms");
    System.out.println("Average bad: " + (totalBad / nbIteration) + " ms");
    System.out.println("\n" + (difference > 0 ? "Gain" : "Loss") + " of " + difference + "%");
}

}
 Validation result Total good: 18461 ms Total bad: 16778 ms Total iteration: 16843082980

Average good: 0 ms Average bad: 0 ms

Loss of -9%