前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >性能优化-测试If和If-Else的性能

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

作者头像
cwl_java
发布2019-11-28 22:48:03
6350
发布2019-11-28 22:48:03
举报
文章被收录于专栏:cwl_Javacwl_Java

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42528266/article/details/103271265

代码示例
代码语言:javascript
复制
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;
    }


}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-11-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档