Rus*_*lan 21 java performance exception instanceof throws
我在变量中有一个异常(没有抛出).
什么是最好的选择?
Exception exception = someObj.getExcp();
try {
throw exception;
} catch (ExceptionExample1 e) {
e.getSomeCustomViolations();
} catch (ExceptionExample2 e) {
e.getSomeOtherCustomViolations();
}
Run Code Online (Sandbox Code Playgroud)
要么
Exception exception = someObj.getExcp();
if (exception instanceof ExceptionExample1) {
exception.getSomeCustomViolations();
} else if (exception instanceof ExceptionExample2) {
exception.getSomeOtherCustomViolations();
}
Run Code Online (Sandbox Code Playgroud)
我建议使用,instanceof
因为它可能会更快.抛出异常是一项复杂而昂贵的操作.在没有发生异常的情况下,JVM被优化为快速.例外应该是例外.
请注意,该throw
技术可能无法编译,如果您的异常类型是一个经过检查的异常,编译器会抱怨您必须捕获该类型或将其声明为抛出(else { ... }
如果您使用该instanceof
技术,则对应于一个子句),这可能或者可能没有帮助,具体取决于您希望如何处理不属于某个特定子类型的异常.
讨厌爆所有人的泡沫,但使用try/catch
是快.这并不是说它是"正确"的方式,但如果表现是关键那么那就是赢家.以下是来自以下计划的结果:
运行1
跑2
跑3
测试环境
折扣每次运行的热身子运行instanceof
方法只能达到最佳性能try/catch
.该方法的平均值(折扣热身)instanceof
为98毫秒,平均值try/catch
为92毫秒.
请注意,我没有改变每种方法的测试顺序.我总是测试一块instanceof
然后块try/catch
.我希望看到其他结果与这些发现相矛盾或得到证实.
public class test {
public static void main (String [] args) throws Exception {
long start = 0L;
int who_cares = 0; // Used to prevent compiler optimization
int tests = 100000;
for ( int i = 0; i < 3; ++i ) {
System.out.println("Testing instanceof");
start = System.currentTimeMillis();
testInstanceOf(who_cares, tests);
System.out.println("instanceof completed in "+(System.currentTimeMillis()-start)+" ms "+who_cares);
System.out.println("Testing try/catch");
start = System.currentTimeMillis();
testTryCatch(who_cares, tests);
System.out.println("try/catch completed in "+(System.currentTimeMillis()-start)+" ms"+who_cares);
}
}
private static int testInstanceOf(int who_cares, int tests) {
for ( int i = 0; i < tests; ++i ) {
Exception ex = (new Tester()).getException();
if ( ex instanceof Ex1 ) {
who_cares = 1;
} else if ( ex instanceof Ex2 ) {
who_cares = 2;
}
}
return who_cares;
}
private static int testTryCatch(int who_cares, int tests) {
for ( int i = 0; i < tests; ++i ) {
Exception ex = (new Tester()).getException();
try {
throw ex;
} catch ( Ex1 ex1 ) {
who_cares = 1;
} catch ( Ex2 ex2 ) {
who_cares = 2;
} catch ( Exception e ) {}
}
return who_cares;
}
private static class Ex1 extends Exception {}
private static class Ex2 extends Exception {}
private static java.util.Random rand = new java.util.Random();
private static class Tester {
private Exception ex;
public Tester() {
if ( rand.nextBoolean() ) {
ex = new Ex1();
} else {
ex = new Ex2();
}
}
public Exception getException() {
return ex;
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19561 次 |
最近记录: |