When reading a book, I think of a sentence, saying that the exception handling of C ++ has an impact on the Running Speed (I saw it several years ago )! Then I thought, what about Java? So it took some time to write the test code, such as package CN. along. java. test;/*** @ author along * @ version 2009-8-18 */public class testexceptionefficiency {public static void main (string [] ARGs) {// first create two exceptionclass objects, to remove the time required for loading the class, it takes predictionclass test1 = new exceptionclass (); predictionclass Test2 = new exceptionclass (); // you can set the value of test1.seti (1000) for both objects ); test2.seti (1000); // start the test of the first object long timebegin1 = system. currenttimemillis (); test1.mytest1 (); // long timeend1 = system. currenttimemillis (); // starts testing the second object (including Exception Handling) Long timebegin2 = system. currenttimemillis (); test2.mytest4 (); long timeend2 = system. currenttimemillis (); // The test is completed. The calculation result is system. out. println ("test1 use time is (time * 1000):" + (timeend1-timebegin1) * 1000) + "timeend =" + timeend1 + "timebegin =" + timebegin1 ); system. out. println ("Test2 use time is (time * 1000):" + (timeend2-timebegin2) * 1000) + "timeend =" + timeend2 + "timebegin =" + timebegin2 ); /*** test1 use time is (time * 1000): 16000 timeend = 1250587016859 timebegin = * 1250587016843
Test2 use time is (time * 1000): 203000 timeend = * 1250587017062 timebegin = 1250587016859 ** if you only declare an exception without throwing an exception. there are few differences in the execution speed between the two. if an exception is thrown, the time of the Test2 object is 10 times. * //} // another file. package CN. along. java. test;/*** @ author along * @ version 2009-8-18 */public class exceptionclass {private int value; Public void SETI (int I) {This. value = I;}/*** first version **/Public void mytest1 () {for (INT I = This. value; I> 0; -- I) {mytest2 (this. value) ;}}/*** calculate the sum of 1 to I: mytest2 (I) = I + mytest2 (I-1) ** @ Param I * @ return */private int mytest2 (int I) {if (I = 1) {return 1;} else {return I + this. mytest2 (I-1) ;}}/*** calculate the sum of 1 to I: mytest3 (I) = I + mytest3 (I-1) ** @ Param I * @ return * @ throws exception * this exception is only for test efficiency */private int mytest3 (int I) throws exception {if (I = 1) {// return 1; throw new exception ("error");} else {return I + this. mytest3 (I-1);}/*** this version only describes, not throwing ** @ Param I * @ return * @ throws exception */private int mytest (int I) throws exception {if (I = 1) {return 1 ;} else {return I + this. mytest3 (I-1) ;}}/*** second version **/Public void mytest4 () {for (INT I = This. value; I> 0; -- I) {try {This. mytest3 (this. value);} catch (exception e) {// E. printstacktrace () ;}}/ *** Test 3 **/Public void mytest5 () {for (INT I = This. value; I> 0; -- I) {try {This. mytest (this. value);} catch (exception e) {// E. printstacktrace () ;}}} has written the conclusion in the main function. I will not describe it here. when an exception is thrown, Java searches backward through the stack to handle catch () blocks. during this period of time, there is no exception.