/ / ¿Puede alguien ayudarme a hacer este código más eficiente? [cerrado] - java, rendimiento, bucle for, optimización, eficiencia de procesamiento

¿Alguien puede ayudarme a hacer que este código sea más eficiente? [cerrado] - java, rendimiento, for-loop, optimización, eficiencia de procesamiento

Estoy creando una calculadora de árbol de habilidades para un juego. Escribí un método que verifica si puede o no restar un punto de la habilidad y luego procede a hacerlo. Todo funciona como debería, sin embargo, es muy lento cuando este método se ejecuta en rápida sucesión (es decir, cuando se restan 5 puntos al mismo tiempo).

¿Puede alguien ayudarme con esto y más importante explicar por qué ciertas cosas son más rápidas y qué evitar?

Aquí está el método:

public void cmdSubtractPoint(int index, SkillTree tree) {

boolean subtract = false;

if (this.blueTree.get(index).getInvestedPoints() != 0) {
//Only subtract a point if the skill isn"t empty

if (index == 0 || index == 1) {
if ((((this.blueTree.get(0).getInvestedPoints() + this.blueTree.get(1).getInvestedPoints()) - 1) < 5)
&& ((this.blueTree.get(2).getInvestedPoints() + this.blueTree.get(3).getInvestedPoints() +
this.blueTree.get(4).getInvestedPoints() + this.blueTree.get(5).getInvestedPoints() + this.blueTree.get(6).getInvestedPoints()) != 0)) {
this.showToast();
} else if ((((this.blueTree.get(0).getInvestedPoints() + this.blueTree.get(1).getInvestedPoints()) + this.blueTree.get(2).getInvestedPoints() + this.blueTree.get(3).getInvestedPoints() - 1) < 10)
&& ((this.blueTree.get(4).getInvestedPoints() + this.blueTree.get(5).getInvestedPoints() + this.blueTree.get(6).getInvestedPoints()) != 0)) {
this.showToast();
} else if ((((this.blueTree.get(0).getInvestedPoints() + this.blueTree.get(1).getInvestedPoints()) + this.blueTree.get(2).getInvestedPoints() + this.blueTree.get(3).getInvestedPoints() + this.blueTree.get(4).getInvestedPoints() + this.blueTree.get(5).getInvestedPoints() - 1) < 15)
&& ((this.blueTree.get(6).getInvestedPoints()) != 0)) {
this.showToast();
} else {
subtract = true;
}
} else if (index == 2 || index == 3) {
if ((((this.blueTree.get(0).getInvestedPoints() + this.blueTree.get(1).getInvestedPoints()) + this.blueTree.get(2).getInvestedPoints() + this.blueTree.get(3).getInvestedPoints() - 1) < 10)
&& ((this.blueTree.get(4).getInvestedPoints() + this.blueTree.get(5).getInvestedPoints() + this.blueTree.get(6).getInvestedPoints()) != 0)) {
this.showToast();
} else if ((((this.blueTree.get(0).getInvestedPoints() + this.blueTree.get(1).getInvestedPoints()) + this.blueTree.get(2).getInvestedPoints() + this.blueTree.get(3).getInvestedPoints() + this.blueTree.get(4).getInvestedPoints() + this.blueTree.get(5).getInvestedPoints() - 1) < 15)
&& ((this.blueTree.get(6).getInvestedPoints()) != 0)) {
this.showToast();
} else {
subtract = true;
}
} else if (index == 4 || index == 5) {
if ((((this.blueTree.get(0).getInvestedPoints() + this.blueTree.get(1).getInvestedPoints()) + this.blueTree.get(2).getInvestedPoints() + this.blueTree.get(3).getInvestedPoints() + this.blueTree.get(4).getInvestedPoints() + this.blueTree.get(5).getInvestedPoints() - 1) < 15)
&& ((this.blueTree.get(6).getInvestedPoints()) != 0)) {
this.showToast();
} else {
subtract = true;
}
} else if (index == 6) {
subtract = true;
}

if (subtract) {
this.pointsInBlueTree -= 1;
this.blueTree.get(index).subtractPoint();
}

}
}

Respuestas

0 para la respuesta № 1

Si reduce el cálculo del valor a una sola vez y usa el interruptor, obtendrá un código similar al siguiente:

public void cmdSubtractPoint(int index, SkillTree tree) {

boolean subtract = false;

if (this.blueTree.get(index).getInvestedPoints() != 0) {
// Only subtract a point if the skill isn"t empty
int temp01 = this.blueTree.get(0).getInvestedPoints() + tree.blueTree.get(1).getInvestedPoints();
int temp23 = this.blueTree.get(2).getInvestedPoints() + tree.blueTree.get(3).getInvestedPoints();
int temp45 = this.blueTree.get(4).getInvestedPoints() + tree.blueTree.get(5).getInvestedPoints();
int temp6 = this.blueTree.get(6).getInvestedPoints();

switch (index) {
case 0:
case 1:
if (((temp01 ) < 6) && ((temp23 + temp45 + temp6) != 0)) {
this.showToast();
} else if (((temp01 + temp23) < 11) && ((temp45 + temp6) != 0)) {
this.showToast();
} else if (((temp01 + temp23 + temp45) < 16) && (temp6 != 0)) {
this.showToast();
} else {
subtract = true;
}
break;
case 2:
case 3:
if (((temp01 + temp23) < 11) && ((temp45 + temp6) != 0)) {
this.showToast();
} else if (((temp01 + temp23 + temp45) < 16) && (temp6 != 0)) {
this.showToast();
} else {
subtract = true;
}
break;
case 4:
case 5:
if (((temp01 + temp23 + temp45) < 16) && (temp6 != 0)) {
this.showToast();
} else {
subtract = true;
}
break;
case 6:
subtract = true;
break;
}

if (subtract) {
this.pointsInBlueTree -= 1;
this.blueTree.get(index).subtractPoint();
}

}
}