¿Cómo optimizar esta función? [cerrado]
Frecuentes
Visto 164 veces
0
Cómo optimizar esta función al máximo
public void r1(String st1, int[] ar1) {
String inox = "newsearch";
for (int j = 0; j < ar1.length; j++) {
if (st1.equals(inox) && ar1[j] * 2 > 20) {
Integer intx = new Integer(ar1[j]);
intx = intx * 2;
System.out.print(intx.toString());
}
}
}
4 Respuestas
6
Este es un código bastante extraño, pero es lo mismo que.
public void r1(String st1, int[] ar1) {
if (!str1.equals("newsearch")) return;
for (int j : ar1) {
int j2 = j * 2;
if (j2 > 20)
System.out.print(j2);
}
}
Respondido el 26 de Septiembre de 12 a las 16:09
reemplazar str1.equals("newsearch") con "newsearch".equals(str1) ayuda a evitar NullPointerException - CAMOBAP
Cierto, pero eso podría haber sido intencionado. ;) - pedro laurey
0
public void r1(String st1, int[] ar1) {
String inox = "newsearch";
if (st1.equals(inox) {
for (int j = 0; j < ar1.length; j++) {
if (ar1[j] > 10) {
System.out.print(ar1[j] * 2);
}
}
}
}
Respondido el 26 de Septiembre de 12 a las 16:09
Esto no es hacer lo mismo. - Assylias
IVA: (1 << 30) * 2 < 0
pero (1 << 30) > 0
- pedro laurey
pero en este caso creo que podemos simplificar - CAMOBAP
0
public void r1(String st1, int[] ar1) {
if (st1.equals("newsearch") {
for (int j = 0; j < ar1.length; j++) {
if (ar1[j] > 10) {
System.out.print(ar1[j] * 2);
}
}
}
}
Respondido el 26 de Septiembre de 12 a las 16:09
0
Construyendo sobre la versión de Peter Lawreys:
public void r1(String st1, int[] ar1) {
if (!str1.equals("newsearch"))
return;
for (int j : ar1)
if (j > 10)
System.out.print(j << 1);
}
Respondido el 26 de Septiembre de 12 a las 16:09
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas java optimization or haz tu propia pregunta.
He optimizado la presentación para ti. ¿Necesitas algo más? (ps: puede usar int en lugar de Integer, eso debería acelerar un poco las cosas) - assylias
¿Qué quieres optimizar? Si lo que menos le preocupa es explicar lo que hace, ¿por qué debería uno molestarse en analizarlo y responderlo? - Amit Deshpande
No veo ninguna optimización obvia. ¿Por qué quieres hacerlo? ¿Es lento? - AnarchoEnte