Java: Are bitwise OR and AND FASTER than the equivalent logical operators?

10,479

Solution 1

Bitwise operators avoid branching instructions, even in Java code execution. As a result you have no expensive branch prediction misses and no jumps at all.

From my experience, they can be measurably faster when used in code that is executed often enough. Keep in mind, though, that the bitwise operators are not short-circuiting ones, which may actually have a negative impact on performance in some cases.

That said, such micro-optimizations should only be used as a last resort and only after a profiler tells you to do so - readability and maintainability comes first.

Solution 2

I suggest you watch Josh Bloch's "Performance Anxiety" talk on Parleys.com. http://www.parleys.com/#st=5&id=2103&sl=1

Solution 3

Most of that would just be optimized by the compiler anyway. A quick Google shows this handy guide to viewing your Java as assembler. I've always been of the opinion that legible, human-readable code is more important than a few fewer nanoseconds of CPU time.

Java isn't the greatest language to pull extreme speed from thanks to the extra layer of the JVM. If you're interested in such precise optimizations, you may want to move to another language such as C/C++. This list shows what languages you may want to look into.

Solution 4

I wonder, would I be better off using bitwise and (&) and bitwise or (|) as opposed to logical operators if possible?

Strange, you went from asking a trivia question about performance to asking if you should actually do it in your code. Well the second one is easy. No. The costs to you as a developer writing less legible code will overwhelm the nanosecond difference in CPU cost. If you need to optimize this much anyway use C or C++.

Solution 5

No.

First, using bitwise operators in contrast to logical operators is prone to error (e.g., doing a right-shift by 1 is NOT equivalent to multiplying by two). Second, the performance benefit is negligible (if any).

Last but not least, using logical operators conveys meaning much better.

Share:
10,479
PinkElephantsOnParade
Author by

PinkElephantsOnParade

I code often.

Updated on June 19, 2022

Comments

  • PinkElephantsOnParade
    PinkElephantsOnParade almost 2 years

    Cut and dry... while I never have enough logical operations for it to be a performance bottleneck - I wonder, would I be better off using bitwise and (&) and bitwise or (|) as opposed to the same-named logical operators (&& and ||) if possible? Perhaps the question can be prefaced by the fact that I don't know of a library to convert Java to assembly to see the # of operations.

  • T.J. Crowder
    T.J. Crowder almost 12 years
    "...overwhelm the nanosecond difference in CPU cost" If even that, +1. "If you need to optimize this much anyway use C or C++." -1 ;-) HotSpot is a freakin' good runtime optimizer.
  • matbrgz
    matbrgz almost 12 years
    Why would the extra layer containing a highly efficient and aggressive compiler mean that extreme speed is not possible?
  • SomeKittens
    SomeKittens almost 12 years
    It's another layer of abstraction. Java loses some speed, but is cross-platform. In most cases, the speed loss is negligible, but if the asker is worried about get a program to run that fast, they may want to check out this list: shootout.alioth.debian.org
  • Sbodd
    Sbodd almost 12 years
    +1 for calling out the functional difference (bitwise ops don't short-circuit)!
  • Louis Wasserman
    Louis Wasserman almost 12 years
    As someone who's optimized code this deeply, yes, it will make a measurable difference in nanoseconds. Not in tens of nanoseconds, though.
  • FireController1847
    FireController1847 over 3 years
    Hey there, I'm interested in learning more about this talk. This link seems to no longer work, do you happen to have another link that shows the same talk?