性能优化-测试If和If-Else的性能

代码示例
package com.cwl.po.judge;

/**
 * @program: cwl-performance-optimization
 * @description: 测试If和If-Else的性能
 * @author: ChenWenLong
 * @create: 2019-11-27 10:34
 **/
public class TestIfElse {

    // 当我们是两种情况进行判断 使用多个If 会比使用If-else要更多消耗一些性能
    public static void main(String[] args) {
        System.out.println(testIf());// 190ms
        System.out.println(testIfElse());// 112ms
    }

    /**
     * 功能描述:
     * 〈测试If-Else〉
     *
     * @params : []
     * @return : long
     * @author : cwl
     * @date : 2019/11/27 10:35
     */
    private static long testIf() {
        long begin = System.currentTimeMillis();
        int a = 0;
        int b = 0;
        for(int i=0;i<100000000;i++){
            if(i % 2 == 0){
                a+=i;
            }
            if(i % 2 == 1){
                b+=i;
            }
        }
        System.out.println(a+b);
        long end = System.currentTimeMillis();
        return end - begin;
    }

    /**
     * 功能描述:
     * 〈测试单纯的使用IF〉
     *
     * @params : []
     * @return : long
     * @author : cwl
     * @date : 2019/11/27 10:34
     */
    private static long testIfElse() {
        long begin = System.currentTimeMillis();
        int a = 0;
        int b = 0;
        for(int i=0;i<100000000;i++){
            if(i % 2 == 0){
                a+=i;
            }else{
                b+=i;
            }
        }
        System.out.println(a+b);
        long end = System.currentTimeMillis();
        return end - begin;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值