本文介绍了Java 8 lambdas和匿名内部类之间的性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在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的,这是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和匿名内部类之间的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!