Java 8 lambdas 和匿名内部类之间的性能差异 [英] Performance difference between Java 8 lambdas and anonymous inner classes
问题描述
在 Java 8 之前,可以通过使用匿名内部类来实现 lambda 功能.例如:
Before Java 8, lambda functionality could be achieved by using anonymous inner classes. For example:
interface Lambda {
void doStuff();
}
// ...
public void doWithCallback(Lambda callback) {
// ...
callback.doStuff();
}
// ...
doWithCallback(new Lambda {
public void doStuff() {
// ...
}
});
在性能方面,仍然使用这种方法和使用新的 Java 8 lambdas 有区别吗?
In terms of performance, is there a difference between still using this approach and using the new Java 8 lambdas?
推荐答案
Oracle 发布了一项研究,比较 Lambdas 和匿名类之间的性能
Oracle has posted a study comparing performance between Lambdas and anonymous classes
请参阅 Sergey Kuksenko 的 JDK 8:Lambda 性能研究,其中有 74 张幻灯片.
See JDK 8: Lambda Performance Study by Sergey Kuksenko, which is 74 slides long.
总结:预热很慢,但当 JIT 内联时,最坏的情况是与匿名类一样快,但可以更快.
Summary: slow to warm up but when JIT inlines it worst case just as fast as anonymous class but can be faster.
这篇关于Java 8 lambdas 和匿名内部类之间的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!