Why System.out::println is slower than anonymous class implementation in Java 8?

[亡魂溺海] 提交于 2019-12-03 21:53:07
apangin

You are measuring the instantiation of method reference, not its runtime performance.

On the first use of method reference (System.out::println) JVM needs to create an internal class which implements IntConsumer interface. Of course, this takes time. Though this is done only once during application lifetime.

In the second case you've made such anonymous class yourself.

If you wish to measure the runtime performance of method references, you have to revise the benchmarking metodology. See "How do I write a correct micro-benchmark in Java?"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!