Optional vs if/else-if performance java 8

前端 未结 5 1656
不思量自难忘°
不思量自难忘° 2021-02-19 23:08

Hello i have two samples of code

if/else if/else statements

private Object getObj(message) {
        if (message         


        
5条回答
  •  不思量自难忘°
    2021-02-19 23:38

    A couple of days ago I ran a thorough performance analysis. There's a huge performance impact. With AdoptOpenJDK, if statements are up to 10 times faster. When the JIT compiler runs hot, this reduces to a 20% penalty.

    GraalVM does a better job: 3 times slowdown with the cold JVM, and after giving the compiler enough time to do its magic, there's also a 20% performance penalty.

    However, the real question is which version is better for reading and maintaining the application. If you're like me, it's easier to read the if statement, but there are also people preferring the functional approach.

    If you're ready for a deep, deep dive, I invite you to read my detailed analysis about the performance and the implementation of Optional.orElseGet() and its friends.

提交回复
热议问题