/**
* 异常效率测试
* @throws Exception
*/
@Test
public void testThrowEfficiency() throws Exception {
long times = 1000000;
long startTime;
System.out.println(times + "循环测试");
try {
recursion(0, true);
}
catch (Exception e) {
System.out.println("异常堆栈深度:" + e.getStackTrace().length);
}
for(int count = 0; count < 10; count++) {
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
try {
recursion(0, true);
}
catch (Exception e) {
}
}
System.out.println("有异常存在:" + (System.currentTimeMillis() - startTime));
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
try {
recursion(0, false);
}
catch (Exception e) {
}
}
System.out.println("无:" + (System.currentTimeMillis() - startTime));
}
}
private void recursion(int count, boolean isThrow) {
if(count == 10) {
if(isThrow) {
throw new IllegalStateException("");
}
return;
}
recursion(count+1, isThrow);
}
public static void main(String[] args) throws Exception {
Test test = new MakeKeyNoTest();
test.testThrowEfficiency();
}