On whether try catch affects performance

Source: Internet
Author: User
Tags try catch

In the actual project, IO, database, network and so on, will inevitably occur unknown exception, try catch can effectively avoid page crashes, online someone said a page four or five try catch affect efficiency, here is the validation:

Example: 100 threads, looping 100 times as an experimental unit:

Package Com.example;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.atomic.atomiclong;/** * @author Xuanyouwu * @email [email protected] * @time 2016-05-06 09:27 *    /public class Trycatchstudy {static Atomiclong taketime;    Static Atomiclong takeTime1; public static void Main (string[] args) throws Exception {Executorservice Executorservice = Executors.newcachedthre        Adpool ();        Taketime = new Atomiclong (0);        takeTime1 = new Atomiclong (0);            for (int j = 0; J <; J + +) {int times = j; Executorservice.execute (New Runnable () {@Override public void run () {Lo                    ng startTime = System.currenttimemillis ();                        for (int i = 0; i <; i++) {try {execute (); } catch (Exception e) {}/* try {                            Thread.Sleep (100); } catch (Exception e) {}*/} long endTime = System.currentti                    Memillis ();                    Taketime.addandget ((endtime-starttime));                    if (times = =) {System.out.println ("------->take Time:" + taketime.get ());        }                }            });            } for (int j = 0; J <; J + +) {int times = j; Executorservice.execute (New Runnable () {@Override public void run () {Lo                    ng startTime1 = System.currenttimemillis ();                    for (int i = 0; i <; i++) {execute ();                        /* Try {thread.sleep (100); } catch (Exception e) {}*/} long endTime1 = System.cuRrenttimemillis ();                    Taketime1.addandget ((endtime1-starttime1));                    if (times = =) {System.out.println ("------->take time2:" + taketime.get ());        }                }            });        }} public static void Execute () {int res = (((INTEGER.MAX_VALUE/2/3-1) * 2/5)-10 * 100)/2;        res--;        ++res;    res = RES/2; }}


Operation Result:

------->take Time:3
------->take Time2:3


Result: no millisecond-level gap


Turn on thread sleep:

Package Com.example;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.atomic.atomiclong;/** * @author Xuanyouwu * @email [email protected] * @time 2016-05-06 09:27 *    /public class Trycatchstudy {static Atomiclong taketime;    Static Atomiclong takeTime1; public static void Main (string[] args) throws Exception {Executorservice Executorservice = Executors.newcachedthre        Adpool ();        Taketime = new Atomiclong (0);        takeTime1 = new Atomiclong (0);            for (int j = 0; J <; J + +) {int times = j; Executorservice.execute (New Runnable () {@Override public void run () {Lo                    ng startTime = System.currenttimemillis ();                        for (int i = 0; i <; i++) {try {execute ();  } catch (Exception e) {} try {                          Thread.Sleep (100); } catch (Exception e) {}} long EndTime = System.currenttime                    Millis ();                    Taketime.addandget ((endtime-starttime));                    if (times = =) {System.out.println ("------->take Time:" + taketime.get ());        }                }            });            } for (int j = 0; J <; J + +) {int times = j; Executorservice.execute (New Runnable () {@Override public void run () {Lo                    ng startTime1 = System.currenttimemillis ();                        for (int i = 0; i <; i++) {execute ();                        try {thread.sleep (100); } catch (Exception e) {}} long endTime1 = System.currenttimEmillis ();                    Taketime1.addandget ((endtime1-starttime1));                    if (times = =) {System.out.println ("------->take time2:" + taketime.get ());        }                }            });        }} public static void Execute () {int res = (((INTEGER.MAX_VALUE/2/3-1) * 2/5)-10 * 100)/2;        res--;        ++res;    res = RES/2; }}


Operation Result:

------->take time2:601414
------->take time:631501


Loop 1 million times:


Package Com.example;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.atomic.atomiclong;/** * @author Xuanyouwu * @email [email protected] * @time 2016-05-06 09:27 *    /public class Trycatchstudy {static Atomiclong taketime;    Static Atomiclong takeTime1; public static void Main (string[] args) throws Exception {Executorservice Executorservice = Executors.newcachedthre        Adpool ();        Taketime = new Atomiclong (0);        takeTime1 = new Atomiclong (0);            for (int j = 0; J <; J + +) {int times = j; Executorservice.execute (New Runnable () {@Override public void run () {Lo                    ng startTime = System.currenttimemillis ();                        for (int i = 0; i < 100_0000; i++) {try {execute ();     } catch (Exception e) {}}               Long endTime = System.currenttimemillis ();                    Taketime.addandget ((endtime-starttime));                    if (times = =) {System.out.println ("------->take Time:" + taketime.get ());        }                }            });            } for (int j = 0; J <; J + +) {int times = j; Executorservice.execute (New Runnable () {@Override public void run () {Lo                    ng startTime1 = System.currenttimemillis ();                    for (int i = 0; i < 100_0000; i++) {execute ();                    } Long endTime1 = System.currenttimemillis ();                    Taketime1.addandget ((endtime1-starttime1));                    if (times = =) {System.out.println ("------->take time2:" + taketime.get ());        }                }            }); }} public staticvoid execute () {int res = (((INTEGER.MAX_VALUE/2/3-1) * 2/5)-10 * 100)/2;        res--;        ++res;    res = RES/2; }}

Operation Result:

------->take time:48
------->take time2:164


Conclusion The general application can completely ignore the efficiency effect of Try catch, when millions of big data concurrency, there is a second gap, the app can be ignored, the background should be considered properly



On whether try catch affects 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.