java 異常效率測試測試

來源:互聯網
上載者:User
看書的時候想起句話,說C++的異常處理對運行速度有影響(好幾年前讀書的時候看到的)!然後就想,那java呢?於是就花了些時間寫了測試,測試的代碼如:package cn.along.java.test;/** * @author along * @version 2009-8-18 */public class TestExceptionEfficiency{public static void main(String[] args){// 首先建立兩個ExceptionClass對象,以去除因載入類而需要的時間花費ExceptionClass test1 = new ExceptionClass();ExceptionClass test2 = new ExceptionClass();// 設定兩個對象的值test1.setI(1000);test2.setI(1000);// 開始第一個對象的測試long timeBegin1 = System.currentTimeMillis();test1.mytest1();//long timeEnd1 = System.currentTimeMillis();// 開始第二個對象的測試(含有異常處理)long timeBegin2 = System.currentTimeMillis();test2.mytest4();long timeEnd2 = System.currentTimeMillis();// 測試結束,計算結果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 * * 如果採用只申明異常,而不拋出異常來看.兩者的執行速度差異性較少.但一但扔出異常,則test2對象的時間將是其10倍. */}}//另一個檔案.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;}/** * 第一個版本 * */public void mytest1(){for (int i = this.value; i > 0; --i){mytest2(this.value);}}/** * 求1到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);}}/** * 求1到i的和 : mytest3(i) = i + mytest3(i -1) * * @param i * @return * @throws Exception * 這個異常只為了測試效率 */private int mytest3(int i) throws Exception{if (i == 1){// return 1;throw new Exception("error");}else{return i + this.mytest3(i - 1);}}/** * 這個版本只說明,並沒有扔出 * * @param i * @return * @throws Exception */private int mytest(int i) throws Exception{if (i == 1){return 1;}else{return i + this.mytest3(i - 1);}}/** * 第二個版本 * */public void mytest4(){for (int i = this.value; i > 0; --i){try{this.mytest3(this.value);}catch (Exception e){// e.printStackTrace();}}}/** * 測試三 * */public void mytest5(){for (int i = this.value; i > 0; --i){try{this.mytest(this.value);}catch (Exception e){// e.printStackTrace();}}}}得出的結論已寫在main函數中了.此處就不再述了.拋出異常之後,之所以佔用大量時間,有書上的解釋是,當異常發生時,java會通過堆棧一路向後尋找,能處理此異常的catch()塊.這一段時間是與不發生異常多出來的.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.