前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >性能优化-测试Contiue是否能对For循环起到的优化作用

性能优化-测试Contiue是否能对For循环起到的优化作用

作者头像
cwl_java
发布2019-11-29 00:35:35
3160
发布2019-11-29 00:35:35
举报
文章被收录于专栏:cwl_Javacwl_Java
代码示例
代码语言:javascript
复制
package com.cwl.po;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: cwl-performance-optimization
 * @description: 测试Contiue是否能对For循环起到的优化作用
 * @author: ChenWenLong
 * @create: 2019-11-22 11:43
 **/
public class TestContinueLoop {

    // 业务场景: for循环遍历集合时总会遇到我们想要的数据,也有不想要的数据参合在一起的时候
    // 我们是使用判定条件将不需要的跳过 或者直接选择想要的数据进行处理,哪一种的性能更好呢
    public static void main(String[] args) {
        System.out.println(testContinue());
        System.out.println(testNotContinue());
        // 结论:最终证明,使用continue并没有是的for循环性能更好
    }

    /**
     * 功能描述:
     * 〈测试for循环中遇到特定条件不使用contiue〉
     *
     * @params : []
     * @return : long
     * @author : cwl
     * @date : 2019/11/22 11:46
     */
    private static long testNotContinue() {
        List<Integer> resultList = new ArrayList<>();
        long begin = System.currentTimeMillis();
        for(int a=0;a<10000000;a++){
            if(a % 2 == 0){
                continue;
            }
            resultList.add(a);
        }
        long end = System.currentTimeMillis();
        return end - begin;
    }

    /**
     * 功能描述:
     * 〈测试for循环中遇到特定条件使用contiue〉
     *
     * @params : []
     * @return : long
     * @author : cwl
     * @date : 2019/11/22 11:47
     */
    private static long testContinue() {
        List<Integer> resultList = new ArrayList<>();
        long begin = System.currentTimeMillis();
        for(int a=0;a<10000000;a++){
            if(a % 2 == 1){
                resultList.add(a);
            }
        }
        long end = System.currentTimeMillis();
        return end - begin;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-11-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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