Why is “while (i++ < n) {}” significantly slower than “while (++i < n) {}”

后端 未结 5 2154
长发绾君心
长发绾君心 2020-12-04 14:16

Apparently on my Windows 8 laptop with HotSpot JDK 1.7.0_45 (with all compiler/VM options set to default), the below loop

final int n = Integer.MAX_VALUE;
in         


        
5条回答
  •  清歌不尽
    2020-12-04 14:24

    I suggest you should (whenever possible) always use ++c rather than c++ as the former will never be slower since, conceptually, a deep copy of c has to be taken in the latter case in order to return the previous value.

    Indeed many optimisers will optimise away an unnecessary deep copy but they can't easily do that if you're making use of the expression value. And you're doing just that in your case.

    Many folk disagree though: they see it as as a micro-optimisation.

提交回复
热议问题