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

后端 未结 5 2306
旧时难觅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.

提交回复
热议问题