foreach loop efficiency for comparison with the normal

/**
 * 测试for与froEach效率
 * @author 15735400536
 *
 */
public class TestList {
	public static void main(String[] args) {
		List<Integer> array = new ArrayList<Integer>();
		List<Integer> link = new LinkedList<Integer>();
		
		long startTime = 0;
		long endTime = 0;
		
		startTime=System.currentTimeMillis();
		for(int i=0; i<100000; i++) {
			array.add(i);
		}
		endTime=System.currentTimeMillis();
		System.out.println("ArrayList add 花费时间: " + (endTime - startTime));
		
		startTime=System.currentTimeMillis();
		for(int i=0; i<100000; i++) {
			link.add(i);
		}
		endTime=System.currentTimeMillis();
		System.out.println("LinkedList add 花费时间: " + (endTime - startTime));
		
		startTime=System.currentTimeMillis();
		for(int i=0; i<100000; i++) {
			array.get(i);
		}
		endTime=System.currentTimeMillis();
		System.out.println("for 遍历  ArrayList get 花费时间: " + (endTime - startTime));
		
		startTime=System.currentTimeMillis();
		for(int i=0; i<100000; i++) {
			link.get(i);
		}
		endTime=System.currentTimeMillis();
		System.out.println("for 遍历 LinkedList get 花费时间: " + (endTime - startTime));
		
		startTime=System.currentTimeMillis();
		for(int i : array) {
			//System.out.println(i);
		}
		= System.currentTimeMillis endTime (); 
		System.out.println ( "forEach time spent traversing ArrayList get:" + (endTime - the startTime)); 
		
		the startTime = System.currentTimeMillis ();
		for (int I: Link) { 
			//System.out.println(i); 
		} 
		endTime = System.currentTimeMillis (); 
		System.out.println ( "forEach traverse LinkedList get to spend time:" + (endTime - startTime) ) ; 
	} 
}

For loop arrayList 10 million times to spend time: 2 milliseconds. With a foreach loop arrayList 10 million times to spend time: 3 ms. For loop linkList 10 million times to spend time: 6163 ms. With a foreach loop linkList 10 million times to spend time: 4 ms.

When the cycle ArrayList, for general circulation than the time it takes to foreach loop a little less. When the cycle LinkList, ordinary time for loop foreach loop takes a lot more than that.

When I was promoted to the number of cycles a million times when circulation ArrayList, ordinary for loop or foreach faster than a little; but for ordinary cycle when the cycle LinkList, the program directly stuck.

ArrayList: ArrayList is in the form of an array of stored objects, the objects in this manner continuous block of memory, it is troublesome inserting or deleting, the query is more convenient.

LinkList: LinkList is an object in a separate space, but also save space in each index of the next space, that is, linked list data structure, insert and delete more convenient, but look for a lot of trouble, from the first beginning traversal.

in conclusion:

When circulation data structure array of needs, it is recommended to use ordinary for loop, because for loop using index access for the data array structure, the adoption of indexed access is better .

Circular list data structure needs must not use the ordinary for loop, this approach is very bad, when there is a large amount of data can cause the system to crash .

Guess you like

Origin www.cnblogs.com/mxh-java/p/11069719.html