Java 异常循环和弃用(或者是 URLEncoding 的东西?)
-
05-09-2019 - |
题
我刚刚写了一个关于我如何达到这一点的完整简介,但我认为发布代码并保留它更容易:)
据我所知, test3() 的性能应该与 test1() 相同 - 唯一的区别是捕获异常的位置(在 test1() 的调用方法内,在 test3() 的被调用方法内)
为什么 test3() 通常需要介于 test1() 和 test2() 之间的时间才能完成?
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");
}
}
运行它会给我(Windows XP 上的 JDK 1.6.0_13)输出:
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
所以,反应非常接近(我们正在谈论一些微不足道、无关紧要的事情),但是我很好奇!
之后...
人们认为 JVM 优化会造成阻碍 - 我同意。因此,我将每个测试分解为它自己的类/主方法,并且每个测试都是单独的。由此得出的结果:
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
有趣的观察:
- 捕获每个调用与捕获循环之外的所有内容之间的差距在其自己的 JVM 中减小了(我认为由于已执行的其他迭代,在一次测试的情况下优化不会全面进行)
- 我这边的 try/catch 与 URLEncoder.encode() 内部的 try/catch 之间的差距现在小得多(5000000 次迭代中半秒),但仍然一致......
解决方案
按照您发布的顺序运行它:
执行了2500000的编码,试图在34208ms中捕获每个编码
执行了2500000的编码,试图在31708ms中捕获全部
在30738ms中执行了2500000的编码
颠倒顺序:
用32598ms的未弃用方法执行了2500000个编码
执行了2500000的编码,试图在31239ms中捕获全部
执行了2500000的编码,试图在31208ms中捕获每个编码
因此,我实际上认为您看到的并不是您认为看到的(当然,test1 并不比 test3 慢 66%,这是您的基准测试所建议的)
其他提示
是的,你有你的解释,我想:
由于涉及额外的方法调用,3 比 1 稍慢。
2 比任何一个都快,因为它不在每个循环中“设置”和“拆除”与异常捕获相关的字节码。你可以破解字节码看看和javap的区别——看 http://www.theserverside.com/tt/articles/article.tss?l=GuideJavaBytecode
使用 2 还是 1 取决于您想要哪种行为,因为它们并不等效。我会选择 1 比 3 因为你没有使用不推荐使用的方法,这比微小的速度增加更重要 - 但碰巧 1 无论如何都会更快。
请纠正我,但是 test2 for 循环只执行了 1 步,因为抛出异常,而 test1 在循环内捕获了异常并执行了 2500000 次。
当你在循环外捕获异常时,循环将不会再次开始。打印出“int i”以确定循环执行了多少步。
第三个是最慢的,因为该方法将调用委托给已弃用的方法。