Which is better option to use for dividing an integer number by 2?

前端 未结 23 1698
甜味超标
甜味超标 2020-11-27 09:10

Which of the following techniques is the best option for dividing an integer by 2 and why?

Technique 1:

x = x >> 1;

Technique

相关标签:
23条回答
  • 2020-11-27 09:49

    I would say there are several things to consider.

    1. Bitshift should be faster, as no special computation is really needed to shift the bits, however as pointed out, there are potential issues with negative numbers. If you are ensured to have positive numbers, and are looking for speed then I would recommend bitshift.

    2. The division operator is very easy for humans to read. So if you are looking for code readability, you could use this. Note that the field of compiler optimization has come a long way, so making code easy to read and understand is good practice.

    3. Depending on the underlying hardware, operations may have different speeds. Amdal's law is to make the common case fast. So you may have hardware that can perform different operations faster than others. For example, multiplying by 0.5 may be faster than dividing by 2. (Granted you may need to take the floor of the multiplication if you wish to enforce integer division).

    If you are after pure performance, I would recommend creating some tests that could do the operations millions of times. Sample the execution several times (your sample size) to determine which one is statistically best with your OS/Hardware/Compiler/Code.

    0 讨论(0)
  • 2020-11-27 09:49

    While x>>1 is faster than x/2, the proper use of >> when dealing with negative values is a little more complicated. It requires something similar to the following:

    // Extension Method
    public static class Global {
        public static int ShiftDivBy2(this int x) {
            return (x < 0 ? x + 1 : x) >> 1;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 09:50

    To pile on: there are so many reasons to favor using x = x / 2; Here are some:

    • it expresses your intent more clearly (assuming you're not dealing with bit twiddling register bits or something)

    • the compiler will reduce this to a shift operation anyway

    • even if the compiler didn't reduce it and chose a slower operation than the shift, the likelihood that this ends up affecting your program's performance in a measurable way is itself vanishingly small (and if it does affect it measurably, then you have an actual reason to use a shift)

    • if the division is going to be part of a larger expression, you're more likely to get the precedence right if you use the division operator:

      x = x / 2 + 5;
      x = x >> 1 + 5;  // not the same as above
      
    • signed arithmetic might complicate things even more than the precedence problem mentioned above

    • to reiterate - the compiler will already do this for you anyway. In fact, it'll convert division by a constant to a series of shifts, adds, and multiplies for all sorts of numbers, not just powers of two. See this question for links to even more information about this.

    In short, you buy nothing by coding a shift when you really mean to multiply or divide, except maybe an increased possibility of introducing a bug. It's been a lifetime since compilers weren't smart enough to optimize this kind of thing to a shift when appropriate.

    0 讨论(0)
  • 2020-11-27 09:50

    x = x / 2; is the suitable code to use.. but an operation depend on your own program of how the output you wanted to produce.

    0 讨论(0)
  • 2020-11-27 09:50

    generaly the right shift divides :

    q = i >> n; is the same as: q = i / 2**n;
    

    this is sometimes used to speed up programs at the cost of clarity. I don't think you should do it . The compiler is smart enough to perform the speedup automatically. This means that putting in a shift gains you nothing at the expense of clarity.

    Take a look at this page from Practical C++ Programming.

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