1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| public class ListTest { static int count = 100000; static int extra = 2000; private static Set<Integer> randomList = new HashSet<>(extra);
static { Random random = new Random(); for(int i = 0; i < extra; i++) { randomList.add(random.nextInt(count)); } }
public static void main(String[] args) { List<String> arrayList = new ArrayList<>(); List<String> linkList = new LinkedList<>(); insert(arrayList); insert(linkList); System.out.println(); insert4Half(arrayList); insert4Half(linkList); System.out.println(); random(arrayList); random(linkList); System.out.println(); randomInsert(arrayList); randomInsert(linkList); System.out.println(); randomRemove(arrayList); randomRemove(linkList); System.out.println(); }
private static void insert(List<String> list) { long start = System.currentTimeMillis(); for(int i = 0; i < count; i++) { list.add("" + i); } long elapsed = System.currentTimeMillis() - start; String prefix = list.getClass().getSimpleName(); System.out.print(" <---> " + String.join("", prefix, "顺序插入耗时ms:", String.valueOf(elapsed))); }
private static void insert4Half(List<String> list) { long start = System.currentTimeMillis(); for(int i = 0; i < extra; i++) { list.add(list.size() / 2, "bbb" + i); } long elapsed = System.currentTimeMillis() - start; String prefix = list.getClass().getSimpleName(); System.out.print(" <---> " + String.join("", prefix, "中间插入耗时ms:", String.valueOf(elapsed))); }
private static void iterate(List<String> list) { long start = System.currentTimeMillis(); for(int i = 0; i < list.size(); i++) { list.get(i); } long elapsed = System.currentTimeMillis() - start; String prefix = list.getClass().getSimpleName(); System.out.print(" <---> " + String.join("", prefix, "顺序遍历耗时ms:", String.valueOf(elapsed))); }
private static void random(List<String> list) { long start = System.currentTimeMillis(); for(Integer i : randomList) { list.get(i.intValue()); } long elapsed = System.currentTimeMillis() - start; String prefix = list.getClass().getSimpleName(); System.out.print(" <---> " + String.join("", prefix, "随机访问耗时ms:", String.valueOf(elapsed))); }
private static void randomInsert(List<String> list) { long start = System.currentTimeMillis(); for(Integer i : randomList) { list.add(i.intValue(), "aa" + i); } long elapsed = System.currentTimeMillis() - start; String prefix = list.getClass().getSimpleName(); System.out.print(" <---> " + String.join("", prefix, "随机插入耗时ms:", String.valueOf(elapsed))); }
private static void randomRemove(List<String> list) { long start = System.currentTimeMillis(); for(Integer i : randomList) { list.remove(i.intValue()); } long elapsed = System.currentTimeMillis() - start; String prefix = list.getClass().getSimpleName(); System.out.print(" <---> " + String.join("", prefix, "随机删除耗时ms:", String.valueOf(elapsed))); } }
|