The impact of Java Try-catch on performance

Source: Internet
Author: User
Tags try catch

Intuitively I feel that the try catch has a performance impact on the program, but the following experiments can show that in some cases try-catch is only a logical effect, with no significant drag on performance

 Public Static voidMain (string[] args)throwsException {intn = 10000;  for(intj = 0; J < 20; J + +) {System.out.println ("Case" + (j + 1)); LongStart =System.currenttimemillis ();  for(inti = 0; I < n; i++) {                Try{testprocess (); } Catch(Exception e) {//Todo:handle Exception}} System.out.println (System.currenttimemillis ()-start); Start=System.currenttimemillis ();  for(inti = 0; I < n; i++) {testprocess (); } System.out.println (System.currenttimemillis ()-start); }    }     Public Static voidtestprocess () {intc = 1;  for(inti = 1; I < 10000; i++) {C*=i; if(c% 2 = = 0) {C+=i; }            if(i = = 0) {                Throw Newruntimeexception (); }        }    }

Results:

 case1  305297case2  285307CASE3  312312case4  306310case5  294296case6  286302case8  276283case9  287291case10  256271case11  256283case13  263287case14  262281case15  254279case16  254281case17  250275case18  262295case20  254288 

The impact of Java Try-catch on performance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.