Why is this code with several “or” statements slightly faster than using a lookup table in Java?

后端 未结 5 2380
旧时难觅i
旧时难觅i 2021-02-20 17:30

While looking at a micro-optimization question that I asked yesterday (here), I found something strange: an or statement in Java is running slightly faste

5条回答
  •  北海茫月
    2021-02-20 18:01

    Loading some random piece of data is generally slower than a little non-branching code.

    It all depends upon processor architecture, of course. Your first if statement could be implemented as four instructions. The second may potentially need null pointer checking, bounds checking as well as the load and compare. Also more code means more compile time, and more chance for the optimisation to be impeeded in some manner.

提交回复
热议问题