Are bitwise rotations slower than shifts on current Intel CPU?

后端 未结 3 504
忘了有多久
忘了有多久 2020-12-11 11:16

I was curious if java.lang.Integer.rotateLeft gets optimized by using a rotation instruction and wrote a benchmark for it. The results were inconclusive: It was

3条回答
  •  失恋的感觉
    2020-12-11 11:53

    When you are looking at micro-benchmarks, you have to consider that the JIT will optimise common patterns e.g. shift, it recognises more efficiently than uncommon patterns e.g. rotate (or ones it does not recognise) This can means that two operations which should take the same amount of time can perform quite differently because one is more heavily optimised than the other. e.g. with more loop unrolling or dead code removal.

    Even simple instructions can interact to produce different and unexpected results. In other words you cannot look at a single instruction and assume it tell you very much about how it will perform when more instructions are used. Context is important at such a low level.

    I suggest you try comparing these operations in a realistic program and I suspect you will have great difficulty finding a measurable difference.

提交回复
热议问题