测试ArrayList和LinkedList性能 发表于 2017-05-08 | 分类于 java | 阅读次数 测试ArrayList和LinkedList性能的案例 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.LinkedList;import java.util.Random;/** * Created by gene on 2017/4/23. */public class Test { public static void main(String[] args) { ArrayList arraylist = new ArrayList(); ArrayList bigarraylist = new ArrayList(5000000); LinkedList linkedlist = new LinkedList(); Object obj = new Object(); /** * 测试ArrayList和LinkedList在末尾插入的性能 */ long start = System.currentTimeMillis(); for (int i = 0; i < 5000000; i++) { arraylist.add(obj); } long end = System.currentTimeMillis(); System.out.println("ArrayList插入用时:" + (end - start)); start=System.currentTimeMillis(); for (int i = 0; i < 5000000; i++) { linkedlist.add(obj); } end = System.currentTimeMillis(); System.out.println("LinkedList插入用时:" + (end - start)); start = System.currentTimeMillis(); for (int i = 0; i < 5000000; i++) { bigarraylist.add(obj); } end = System.currentTimeMillis(); System.out.println("调整初始容量的arrayList插入用时:" + (end - start)); /** * 测试ArrayList和LinkedList随机插入的性能 */ Random r1 = new Random(); Random r2 = new Random(); start = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { arraylist.add(r1.nextInt(1000), obj); } end = System.currentTimeMillis(); System.out.println("ArrayList随机插入用时:" + (end - start)); LinkedHashMap map = new LinkedHashMap(); map.put(1, 1); map.put(2, 2); start = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { linkedlist.add(r2.nextInt(1000), obj); } end = System.currentTimeMillis(); System.out.println("LinkedList随机插入用时:" + (end - start)); /** * 测试ArrayList和LinkedList的删除性能 */ start = System.currentTimeMillis(); for (int i = 0; i < 100; i++) { arraylist.remove(250000); } end = System.currentTimeMillis(); System.out.println("ArrayList删除用时:" + (end - start)); start = System.currentTimeMillis(); for (int i = 0; i < 100; i++) { linkedlist.remove(250000); } end = System.currentTimeMillis(); System.out.println("LinkedList删除用时:" + (end - start)); }}