Android开发中编码性能问题之for-each

博客探讨了在Android开发中,for-each循环(增强型循环)与传统循环在遍历ArrayList和List时的性能差异。实验结果显示,对于ArrayList和List,普通循环的效率高于for-each循环,但在遍历其他数据结构时,for-each可能更优。建议在遍历ArrayList和List时使用普通循环。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在以往的开发中有些东西没有注意到,这里特意记录下。
for-each增强型循环,做开发的都不会陌生,简单来说for-each比较简洁提高遍历效率。以往我们遍历循环都是如下:

List<String> testList = new ArrayList<String>();
        for(int i = 0; i < testList.size(); i++){
            System.out.print("test: "+testList.get(i));
        }

普遍认为提高效率写法for-each

List<String> testList = new ArrayList<String>();
        for(String str : testList){
            System.out.print("test: " +str);
        }

自从学编程开始,很多人都觉得for-each比较简洁方便效率高,所以很多时候潜意识里也认为for-each真的效率高,我以前也没注意这个,在做android开发的时候基本上很多地方用的for-each,但真的for-each效率比普通循环效率要高?看下面测试结果

new Thread(new Runnable() {

            @Override
            public void run() {
                // arraylist 测试
                List<String> list = new ArrayList<String>();
                for(int i = 0; i< 10000 ;i ++){
                    list.add("de: "+i);
                }
                // for eacth
                String strddd = "";
                long ecathSystemTime = System.currentTimeMillis();
                for(String str: list){
                    strddd = strddd + str;
                }
                System.out.println("eacth time : " + (System.currentTimeMillis() - ecathSystemTime));

                String dddStr = "";
                ecathSystemTime = System.currentTimeMillis();
                for (int i = 0 ; i < list.size(); i++){
                    dddStr = dddStr + list.get(i);
                }
                System.out.println("for time : " + (System.currentTimeMillis() - ecathSystemTime));


                // ---------数组测试
                String[] arrayTest = new String[10000];
                for(int i = 0; i< arrayTest.length ; i++){
                    arrayTest[i] = "test" + i;
                }

                // for eacth
                String arrayStr = "";
                ecathSystemTime = System.currentTimeMillis();
                for(String str: arrayTest){
                    arrayStr = arrayStr + str;
                }
                System.out.println("array eacth time : " + (System.currentTimeMillis() - ecathSystemTime));

                String arrayddStr = "";
                ecathSystemTime = System.currentTimeMillis();
                for (int i = 0 ; i < list.size(); i++){
                    arrayddStr = arrayddStr + list.get(i);
                }
                System.out.println("array for time : " + (System.currentTimeMillis() - ecathSystemTime));
            }
        }).start();

这里写图片描述

是不是颠覆了很多人潜意识里的for-each?在遍历List,ArrayList时,for-each的效率远远比不上普通的循环,而遍历其它方式时for-each的效率是比普通循环效率要快的。所以,在遍历ArrayList, List时请用普通的循环,请用普通的循环,请用普通的循环!重要的事情说三遍…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值