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

后端 未结 5 2394
旧时难觅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 17:48

    In the current example, I agree that bounds checking is probably what's getting you (why the JVM doesn't optimize this out is beyond me - the sample code could can deterministically be shown to not overflow...

    Another possibility (especially with bigger lookup tables) is cache latency... It depends on the size of the processors' registers and how the JVM chooses to use them - but if the byte array isn't kept totally on processor, then you'll see a performance hit compared to a simple OR as the array is pulled onto the CPU for each check.

    0 讨论(0)
  • 2021-02-20 17:50

    It's an interesting piece of code, but 2% is a really small difference. I don't think you can conclude very much from that.

    0 讨论(0)
  • 2021-02-20 17:53

    I would guess that the issues is that range checking for the array and if the array lookup is implemented as a method call. That would certainly overshadow 4 straight int compares. Have you looked at the byte code?

    0 讨论(0)
  • 2021-02-20 17:59

    According to this article accessing array elements are "2 or 3 times as expensive as accessing non-array elements". Your test shows that the difference may be even bigger.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题