package ch09_04;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class ArrayList_LinkedListTest {
	//基于List类型的集合添加n个元素所需要的时间
	static long addListTime(List<Object> list,long n) {
		long start = System.currentTimeMillis();
		Object o = new Object();
		for (int i = 0; i < n; i++){
			list.add(0, o);
	      }
		return System.currentTimeMillis() - start;
	}
	//基于List类型的集合，遍历集合中前n个元素所需要的时间
	static long findListTime(List<Object> list,long n) {
		long start = System.currentTimeMillis();
		for(int i=0;i<n;i++){
			list.get(i);
		}
		return System.currentTimeMillis() - start;
	}
	public static void main(String[] args) {
		List<Object> arrayList=new ArrayList<Object>();
		List<Object> linkList=new LinkedList<Object>();
		long t1=addListTime(arrayList,100000);
		long t2= addListTime(linkList,100000);
		System.out.println("ArrayList在头部插入元素耗时：" +t1 );
		System.out.println("LinkedList在头部插入元素耗时：" +t2);
		System.out.println("ArrayList查找元素耗时：" + findListTime(arrayList,100000));
		System.out.println("LinkedList查找元素耗时：" + findListTime(linkList,100000));
	}
}
