zoukankan      html  css  js  c++  java
  • list_arrayList三种遍历性能比较

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


    public class ListTest {

        /**
         * @param args
         */
        public static void main(String[] args) {
            List<Integer> list = new ArrayList<Integer>();
            for(int j=0;j<1000000;j++){
                list.add(j);
            }
            long t1,t2;
            t1=System.currentTimeMillis();          
            for(int i = 0;i<list.size();i++){
                int a = list.get(i);
            }
            t2=System.currentTimeMillis();  
            System.out.println("Run1 Time:" + (t2 -t1) + "(ms)");  
            
            
            System.out.println("---------------------------");
            t1=System.currentTimeMillis();
            for(int listValue:list){
                int b = listValue;
            }
            t2=System.currentTimeMillis();  
            System.out.println("Run2 Time:" + (t2 -t1) + "(ms)");
            System.out.println("---------------------------");
            t1=System.currentTimeMillis();
            Iterator<Integer> iterato = list.iterator();
            while(iterato.hasNext()){
                int c = iterato.next();
            }
            t2=System.currentTimeMillis();  
            System.out.println("Run3 Time:" + (t2 -t1) + "(ms)");

        }

    }

    result:Run1 Time:13(ms)
    ---------------------------
    Run2 Time:34(ms)
    ---------------------------
    Run3 Time:29(ms)

    从结果可以看出第一种遍历速度最快

  • 相关阅读:
    webstorm 自定义代码模板
    HTML5 manifest ApplicationCache
    WebStorm 快捷键收藏
    函数内巧用注释实现多行文本拼接
    图片剪裁上传插件
    将json转为复杂url参数
    CSS3实现半像素边框
    打造自己的3D全景漫游
    自适应rem布局
    header页头内容整理
  • 原文地址:https://www.cnblogs.com/gtaxmjld/p/4623683.html
Copyright © 2011-2022 走看看