Je viens d'écrire un texte de présentation tout sur la façon dont je suis arrivé à ce point, mais figurés il est plus facile d'afficher le code et le laisser à ce :)Java Exceptions Les boucles et désapprobations (ou est-ce une chose urlencodage?)
Comme autant que je peux dire, la performance de test3() devrait être la même que test1() - la seule différence est où l'exception est attrapée (à l'intérieur de la méthode appelant pour test1(), à l'intérieur de la méthode appelée pour test3())
Pourquoi test3() prend-il régulièrement du temps entre test1() et test2() pour terminer?
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class Test {
public static void main(String[] args) {
warmup();
test1(2500000); // Exception caught inside the loop
test2(2500000); // Exception caught outside the loop
test3(2500000); // Exception caught "inside" the loop, but in the URLEncoder.encode() method
}
private static void warmup() {
// Let URLEncoder do whatever startup it needs before we hit it
String encoding = System.getProperty("file.encoding");
try {
URLEncoder.encode("ignore", encoding);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void test1(int count) {
String encoding = System.getProperty("file.encoding");
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
try {
URLEncoder.encode("test 1 " + i, encoding);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("Performed " + count + " encodings trying to catch each in " + (end - start) + "ms");
}
private static void test2(int count) {
String encoding = System.getProperty("file.encoding");
long start = System.currentTimeMillis();
try {
for (int i = 0; i < count; i++) {
URLEncoder.encode("test 2" + i, encoding);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("Performed " + count + " encodings trying to catch all in " + (end - start) + "ms");
}
private static void test3(int count) {
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
URLEncoder.encode("test 3 " + i);
}
long end = System.currentTimeMillis();
System.out.println("Performed " + count + " encodings with a deprecated method in " + (end - start) + "ms");
}
}
Courir ça me donne (JDK 1.6.0_13 sous Windows XP) la sortie:
Performed 2500000 encodings trying to catch each in 4906ms
Performed 2500000 encodings trying to catch all in 2454ms
Performed 2500000 encodings with a deprecated method in 2953ms
Ainsi, les réponses sont assez proches (on parle quelque chose qui est si trivial qu'il est hors de propos), btu Je suis curieux!
plus tard ...
Les gens ont suggéré qu'il ya optimisation JVM obtenir de la manière - je suis d'accord. J'ai donc divisé chaque test en fonction de sa propre classe/méthode principale et individuellement. Les résultats de cette:
1 - Performed 2500000 encodings trying to catch each in 5016ms
1 - Performed 5000000 encodings trying to catch each in 7547ms
1 - Performed 5000000 encodings trying to catch each in 7515ms
1 - Performed 5000000 encodings trying to catch each in 7531ms
2 - Performed 2500000 encodings trying to catch all in 4719ms
2 - Performed 5000000 encodings trying to catch all in 7250ms
2 - Performed 5000000 encodings trying to catch all in 7203ms
2 - Performed 5000000 encodings trying to catch all in 7250ms
3 - Performed 2500000 encodings with a deprecated method in 5297ms
3 - Performed 5000000 encodings with a deprecated method in 8015ms
3 - Performed 5000000 encodings with a deprecated method in 8063ms
3 - Performed 5000000 encodings with a deprecated method in 8219ms
observations intéressantes:
- L'écart entre la capture chaque appel contre la capture tout à l'extérieur de la boucle est réduite dans sa propre machine virtuelle Java (je suppose que l'optimisation ne va pas tout le porc dans le cas de tous les tests en un en raison des autres itérations qui ont été effectuées)
- L'écart entre le try/catch de mon côté et le try/catch dans URLEncoder.encode() est maintenant beaucoup plus petit (moitié une seconde sur 5000000 itérations) mais toujours de manière constante ...
Essayez les tests dans un ordre différent: test2(), test3(), test1() pour isoler les différences dues à la compilation JIT. – erickson