正如我所发现的,使用Stream迭代数组的速度要慢得多(74张幻灯片不考虑这种情况)。我认为它不是lambdas中唯一的性能泄漏(猜测,将来会有所改善)。下面的示例使用 Java 8 运行,没有任何选项:
//Language is an enum
Language[] array = Language.values();
System.err.println(array.length); // 72 items
long t = System.nanoTime();
for (Language l : array) System.out.println(l.getLanguageName());
System.err.println(System.nanoTime()-t); //nano time 1864724
t = System.nanoTime();
Arrays.stream(array).forEach(v -> System.out.println(v.getLanguageName()));
System.err.println(System.nanoTime()-t); //nano time 55812625 (55812625/1864724 = 29.93 times longer)
List<Language> list = Arrays.asList(array);
t = System.nanoTime();
for (Language l : list) System.out.println(l.getLanguageName());
System.err.println(System.nanoTime()-t); //nano time 1435008
t = System.nanoTime();
list.forEach(v -> System.out.println(v.getLanguageName()));
System.err.println(System.nanoTime()-t); //nano time 1619973 (1619973/1435008 = 1.128 times longer)