java for与forEach 效率

jdk 6

package com.howtodoinjava.demo.core;
 
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
 
public class ForLoopPerformanceTest
{
    private static List<Integer> list = new ArrayList<Integer>();
    private static long startTime;
    private static long endTime;
    
//    static
//    {
//        for(int i=0; i < 10000000; i++)
//        {
//            list.add(i);
//        }
//    }
    
    public static void main(String[] args) {
    	
    	 for(int n=0; n < 1000000; n++)
         {
             list.add(n);
         }
		
        //Type 1
        startTime = Calendar.getInstance().getTimeInMillis();
        for(Integer i : list)
        {
            //
        }
        endTime = Calendar.getInstance().getTimeInMillis();
        System.out.println("For each loop :: " + (endTime - startTime) + " ms");
 
        //Type 2
        startTime = Calendar.getInstance().getTimeInMillis();
        for(int j = 0; j < list.size() ; j++)
        {
            //
        }
        endTime = Calendar.getInstance().getTimeInMillis();
        System.out.println("Using collection.size() :: " + (endTime - startTime) + " ms");
 
        //Type 3
        startTime = Calendar.getInstance().getTimeInMillis();
        int size = list.size();
        for(int j = 0; j < size ; j++)
        {
            //System.out.println(j);
        }
        endTime = Calendar.getInstance().getTimeInMillis();
        System.out.println("Using [int size = list.size(); int j = 0; j < size ; j++] :: " + (endTime - startTime) + " ms");
 
        //Type 4
        startTime = Calendar.getInstance().getTimeInMillis();
        for(int j = list.size(); j > 0 ; j--)
        {
            //System.out.println(j);
        }
        endTime = Calendar.getInstance().getTimeInMillis();
        System.out.println("Using [int j = list.size(); j > size ; j--] :: " + (endTime - startTime) + " ms");
    }
}

 结果:

For each loop :: 64 ms
Using collection.size() :: 16 ms
Using [int size = list.size(); int j = 0; j < size ; j++] :: 1 ms
Using [int j = list.size(); j > size ; j--] :: 1 ms

 结论:第一种方法的效率远远低于其余三种(Clearly the last two ways are way ahead in terms of performance, while for each statement [type 1] is most expensive operation if compared with other three.)

 

Reason for difference in performance

Last two flavors type 3 and 4 have a very little difference and should be considered as same. They both fetch the size of collection initially. And then uses this size value in loop for checking the condition.

Type 2 uses size() method call every time and thus on runtime it brings a little overhead. Though JVM optimizes this code as inline call and other optimizations also and size method is simply a getter for size attribute of instance of list. Even though it brings a few more statements to execute at machine level code and which makes the difference.

Type 1 is costliest one and simple reasoning is the use of iterator internally created in for each loop. Creating an iterator and calling iterator.get() adds up to most of cost which is not involved in direct access in other three types.

 

 

http://howtodoinjava.com/2013/03/26/performance-comparison-of-different-for-loops-in-java/

 

 

对ArrayList这样的可使用下标进行随机访问的数据结构,使用下标访问,要比foreach的方式进行顺序访问,速度要快一些。foreach这样写法,使用的过程产生一个额外的对象Enumerator,而且每次访问需要更多的操作,降低性能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值