为什么枚举比Iterator快?虽然我们在迭代器和枚举方面有更多的优势。
答案 0 :(得分:1)
值得注意的是,Enumeration仅适用于较旧的线程安全集合。使用Iterator可以在任何集合上使用,因为它不必是线程安全的,所以没有额外的锁定开销。
public static void main(String... args) {
Vector<Integer> vec = new Vector<>();
ArrayList<Integer> al = new ArrayList<>();
polulate(vec);
polulate(al);
for (int i = 0; i < 5; i++) {
long start = System.nanoTime();
long sum1 = enumSumOf(vec);
long mid1 = System.nanoTime();
long sum1b = iterSumOf(vec);
long mid2 = System.nanoTime();
long sum2 = iterSumOf(al);
long end = System.nanoTime();
System.out.printf("Each iteration of Enumeration took %,d ns (Vector), Iteration took %,d ns (Vector), Iteration took %,d ns (ArrayList)%n",
(mid1 - start) / vec.size(), (mid2 - mid1) / vec.size(), (end - mid2) / al.size());
}
}
private static long enumSumOf(Vector<Integer> vec) {
long sum = 0;
for (Enumeration<Integer> e = vec.elements(); e.hasMoreElements(); )
sum += e.nextElement();
return sum;
}
private static long iterSumOf(Vector<Integer> al) {
long sum = 0;
for (int i : al)
sum += i;
return sum;
}
private static long iterSumOf(ArrayList<Integer> al) {
long sum = 0;
for (int i : al)
sum += i;
return sum;
}
private static void polulate(List<Integer> l) {
for (int i = 0; i < 1000000; i++)
l.add(i);
}
打印
Each iteration of Enumeration took 61 ns (Vector),
Iteration took 74 ns (Vector), Iteration took 17 ns (ArrayList)
Each iteration of Enumeration took 52 ns (Vector),
Iteration took 54 ns (Vector), Iteration took 6 ns (ArrayList)
Each iteration of Enumeration took 52 ns (Vector),
Iteration took 54 ns (Vector), Iteration took 6 ns (ArrayList)
Each iteration of Enumeration took 52 ns (Vector),
Iteration took 53 ns (Vector), Iteration took 6 ns (ArrayList)
Each iteration of Enumeration took 40 ns (Vector),
Iteration took 36 ns (Vector), Iteration took 5 ns (ArrayList)
在给出的微基准测试的例子中,枚举比Iterator快的原因是因为它首先被测试。 ;)
更长的答案是,当循环迭代10,000次时,HotSpot编译器会优化整个方法。您可以使用-XX:CompileThreshold=NNNN
更改此设置当您有两个循环时,您可以看到相同的方法。
显示这种情况的一种简单方法是交换循环的顺序。更好的解决方案是将每个循环放在自己的方法中并重复测试几次。这可确保它们独立优化,结果可重复。