What is faster (x < 0) or (x == -1)?

前端 未结 12 2041
悲哀的现实
悲哀的现实 2021-02-01 02:56

Variable x is int with possible values: -1, 0, 1, 2, 3. Which expression will be faster (in CPU ticks):

1. (x < 0)
2. (x == -1)


        
相关标签:
12条回答
  • 2021-02-01 03:24

    x < 0 will be faster. If nothing else, it prevents fetching the constant -1 as an operand. Most architectures have special instructions for comparing against zero, so that will help too.

    0 讨论(0)
  • 2021-02-01 03:25

    That depends entirely on the ISA you're compiling for, and the quality of your compiler's optimizer. Don't optimize prematurely: profile first to find your bottlenecks.

    That said, in x86, you'll find that both are equally fast in most cases. In both cases, you'll have a comparison (cmp) and a conditional jump (jCC) instructions. However, for (x < 0), there may be some instances where the compiler can elide the cmp instruction, speeding up your code by one whole cycle.

    Specifically, if the value x is stored in a register and was recently the result of an arithmetic operation (such as add, or sub, but there are many more possibilities) that sets the sign flag SF in the EFLAGS register, then there's no need for the cmp instruction, and the compiler can emit just a js instruction. There's no simple jCC instruction that jumps when the input was -1.

    0 讨论(0)
  • 2021-02-01 03:32

    You can't even answer this question out of context. If you try for a trivial microbenchmark, it's entirely possible that the optimizer will waft your code into the ether:

    // Get time
    int x = -1;
    for (int i = 0; i < ONE_JILLION; i++) {
        int dummy = (x < 0); // Poof!  Dummy is ignored.
    }
    // Compute time difference - in the presence of good optimization
    // expect this time difference to be close to useless.
    
    0 讨论(0)
  • 2021-02-01 03:33

    The important consideration, anyway, is which actually directs your program flow accurately, and which just happens to produce the same result?

    If x is actually and index or a value in an enum, then will -1 always be what you want, or will any negative value work? Right now, -1 is the only negative, but that could change.

    0 讨论(0)
  • 2021-02-01 03:34

    Try it and see! Do a million, or better, a billion of each and time them. I bet there is no statistical significance in your results, but who knows -- maybe on your platform and compiler, you might find a result.

    This is a great experiment to convince yourself that premature optimization is probably not worth your time--and may well be "the root of all evil--at least in programming".

    0 讨论(0)
  • 2021-02-01 03:43

    As others have said there probably isn't any difference. Comparisons are such fundamental operations in a CPU that chip designers want to make them as fast as possible.

    But there is something else you could consider. Analyze the frequencies of each value and have the comparisons in that order. This could save you quite a few cycles. Of course you still need to compile your code to asm to verify this.

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