Explain Codes LogoExplain Codes Logo

Why is (a*b != 0) faster than (a != 0 && b != 0) in Java?

java
branch-prediction
performance-optimization
jvm-optimizations
Nikita BarsukovbyNikita Barsukov·Mar 13, 2025
TLDR

The paradoxical performance trickster (a*b != 0) steals the show over (a != 0 && b != 0) by wrapping up a possible duo of comparisons into a single multiplication and comparison act. As a result, it halves the number of associated conditional branches while maintaining a steady arithmetic performance stride, thus outrunning the subsequent branch prediction overhead and enhancing execution time.

// Drum roll please... And the winner is boolean isNonZero = (a * b != 0);

Nonetheless, watch your step for concealed integer overflow traps. Multiplying mammoth numbers can result in an overflow, fooling the checker into a false non-zero.

Unwrapping the Performance Mystery

The Enigma of Branch Prediction

When a branch like && presents, the CPU ventures a guess on the execution direction. If the forecast goes awry, the CPU pays a penalty and restarts the guessing game. Adding a few more "guess-work" results in an unwanted performance hit.

Beyond the (a*b != 0) Realm: Contemplating Alternatives

Amid the non-negative integers, the expression (a|b) != 0 offers an overflow-immune bitwise option. (a+b) != 0 is yet another option, with an overflow-risk akin to multiplication. Both present swift single-check alternatives.

System Preferences: Systems, JVMs and Benchmarks

It's vital to note that performance measurements oscillate across different JVMs and hardware configurations. Embrace System.nanoTime() for precise timing benchmarks and VM disassembly to delve into JIT compiler influenced optimizations.

Living the Optimized Life: Real-world Adaptations and Implications

Unswerving predictability of branches can thwart the momentum gained from single-check optimization. Code paths post-checks greatly influences how much real-world juice you’ll extract from these optimizations. Also, impressive micro-benchmarks often fail to garner the same applause in a live application scenario.