Is Math.max(a,b) or (a>b)?a:b faster in Java?

后端 未结 7 1576
深忆病人
深忆病人 2020-12-03 20:59

Which one is faster in Java and why?

  1. Math.max(a,b)
  2. (a>b)?a:b

(This was asked in an interview.)

7条回答
  •  醉话见心
    2020-12-03 21:28

    Math.max(a, b) is a static function (meaning no virtual call overhead) and will likely be inlined by the JVM to the same instructions as (a > b) ? a : b.

提交回复
热议问题