论 try catch是否影响性能

在实际项目中,io,数据库,网络等等,不可避免会发生未知异常,try catch 可以有效的避免页面崩溃,网上有人说一个页四五个try catch影响效率,这里给出验证:

实例:100个线程,分别循环100次作为实验单位:

package com.example;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @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.newCachedThreadPool();
        takeTime = new AtomicLong(0);
        takeTime1 = new AtomicLong(0);
        for (int j = 0; j < 100; j++) {
            int times = j;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    long startTime = System.currentTimeMillis();
                    for (int i = 0; i < 100; i++) {
                        try {
                            execute();
                        } catch (Exception e) {
                        }
                       /* try {
                            Thread.sleep(100);
                        } catch (Exception e) {
                        }*/
                    }
                    long endTime = System.currentTimeMillis();
                    takeTime.addAndGet((endTime - startTime));
                    if (times == 99) {
                        System.out.println("------->take time:" + takeTime.get());
                    }
                }
            });
        }


        for (int j = 0; j < 100; j++) {
            int times = j;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    long startTime1 = System.currentTimeMillis();
                    for (int i = 0; i < 100; i++) {
                        execute();
                    /*    try {
                            Thread.sleep(100);
                        } catch (Exception e) {
                        }*/
                    }
                    long endTime1 = System.currentTimeMillis();
                    takeTime1.addAndGet((endTime1 - startTime1));
                    if (times == 99) {
                        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;
    }
}


运行结果:

------->take time:3
------->take time2:3


结果:没有毫秒级别的差距


开启线程睡眠:

package com.example;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @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.newCachedThreadPool();
        takeTime = new AtomicLong(0);
        takeTime1 = new AtomicLong(0);
        for (int j = 0; j < 100; j++) {
            int times = j;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    long startTime = System.currentTimeMillis();
                    for (int i = 0; i < 100; i++) {
                        try {
                            execute();
                        } catch (Exception e) {
                        }
                        try {
                            Thread.sleep(100);
                        } catch (Exception e) {
                        }
                    }
                    long endTime = System.currentTimeMillis();
                    takeTime.addAndGet((endTime - startTime));
                    if (times == 99) {
                        System.out.println("------->take time:" + takeTime.get());
                    }
                }
            });
        }


        for (int j = 0; j < 100; j++) {
            int times = j;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    long startTime1 = System.currentTimeMillis();
                    for (int i = 0; i < 100; i++) {
                        execute();
                        try {
                            Thread.sleep(100);
                        } catch (Exception e) {
                        }
                    }
                    long endTime1 = System.currentTimeMillis();
                    takeTime1.addAndGet((endTime1 - startTime1));
                    if (times == 99) {
                        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;
    }
}


运行结果:

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


循环100万次:


package com.example;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @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.newCachedThreadPool();
        takeTime = new AtomicLong(0);
        takeTime1 = new AtomicLong(0);
        for (int j = 0; j < 100; j++) {
            int times = j;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    long 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 == 99) {
                        System.out.println("------->take time:" + takeTime.get());
                    }
                }
            });
        }


        for (int j = 0; j < 100; j++) {
            int times = j;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    long startTime1 = System.currentTimeMillis();
                    for (int i = 0; i < 100_0000; i++) {
                        execute();
                    }
                    long endTime1 = System.currentTimeMillis();
                    takeTime1.addAndGet((endTime1 - startTime1));
                    if (times == 99) {
                        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;
    }
}

运行结果:

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


结论 一般应用完全可以忽略try catch带来的效率影响,当百万计的大数据并发,有秒的差距,app可以忽略,后台要适当考虑



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亚洲小炫风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值