首页 > 其他 > 详细

ArrayList的不同的遍历方式性能比较,其实都差不了多少

时间:2017-09-02 23:14:52      阅读:285      评论:0      收藏:0      [点我收藏+]
//RadomAcess的接口for..i的遍历比for..loop的快,@more see comments for interface RandomAcess 
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
public class ListPerformanceTest {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        for(int i = 0; i < Integer.MAX_VALUE / 200; i++) {
            list.add("1");
        }
        long d1 = System.currentTimeMillis();
        /***************** fastest ************************/
//        for(int i = 0, n = list.size(); i < n; i++) {
//            list.get(i);
//        }
        /***************** second fastest due to each loop would get the list size ************************/
//        for(int i = 0; i < list.size(); i++) {
//            list.get(i);
//        }
        /***************** not know why this is faster than the last ************************/
//        for(Iterator<?> it = list.iterator(); it.hasNext();) {
//            it.next();
//        }
 
//        for(String s : list) {
//            if(s != null) {
//                continue;
//            }
//        }
        System.out.println("Times(ms):" + (System.currentTimeMillis() - d1));
    }
}

ArrayList的不同的遍历方式性能比较,其实都差不了多少

原文:http://www.cnblogs.com/superuni/p/7468186.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!