Performance wise, how fast are Bitwise Operators vs. Normal Modulus?

前端 未结 8 1818
无人及你
无人及你 2020-12-01 04:15

Does using bitwise operations in normal flow or conditional statements like for, if, and so on increase overall performance and would it be better

8条回答
  •  春和景丽
    2020-12-01 04:51

    TL;DR Write for semantics first, optimize measured hot-spots second.

    At the CPU level, integer modulus and divisions are among the slowest operations. But you are not writing at the CPU level, instead you write in C++, which your compiler translates to an Intermediate Representation, which finally is translated into assembly according to the model of CPU for which you are compiling.

    In this process, the compiler will apply Peephole Optimizations, among which figure Strength Reduction Optimizations such as (courtesy of Wikipedia):

    Original Calculation  Replacement Calculation
    y = x / 8             y = x >> 3
    y = x * 64            y = x << 6
    y = x * 2             y = x << 1
    y = x * 15            y = (x << 4) - x
    

    The last example is perhaps the most interesting one. Whilst multiplying or dividing by powers of 2 is easily converted (manually) into bit-shifts operations, the compiler is generally taught to perform even smarter transformations that you would probably think about on your own and who are not as easily recognized (at the very least, I do not personally immediately recognize that (x << 4) - x means x * 15).

提交回复
热议问题