Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AntoineMeheut opened this issue May 30, 2024 · 0 comments
Assignees
Labels
checkers Hackathon 2024 New issues tagged during the hackathon 2024

Comments

@AntoineMeheut
Copy link
Contributor

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%


@AntoineMeheut AntoineMeheut added checkers Hackathon 2024 New issues tagged during the hackathon 2024 labels May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
checkers Hackathon 2024 New issues tagged during the hackathon 2024
Projects
None yet
Development

No branches or pull requests

2 participants