Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

Language: C/C++, but I suppose all other languages will have the same.

P.S. I personally think that answer is (x < 0).

More widely for gurus: what if x from -1 to 2^30?

like image 834
Nikolay Vyahhi Avatar asked May 26 '09 19:05

Nikolay Vyahhi


People also ask

Why === is faster than ==?

Equality operator == converts the data type temporarily to see if its value is equal to the other operand, whereas === (the identity operator) doesn't need to do any type casting and thus less work is done, which makes it faster than ==.

Which comparison operator is faster?

Some processors are quicker when comparing against zero. So > 0 might be faster than >= 1 , as an example. @Simple A decent compiler will replace >= 1 with > 0 if it's faster.

Which operations are faster?

D. Explanation: combinational circuits are often faster than sequential circuits. since, the combinational circuits do not require memory elements whereas the sequential circuits need memory devices to perform their operations in sequence. latches and flip-flops come under sequential circuits.

Is x x 1 the same as x+= 1?

They are the same. Show activity on this post.


8 Answers

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.

like image 125
Adam Rosenfield Avatar answered Oct 03 '22 01:10

Adam Rosenfield


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".

like image 44
Alex Feinman Avatar answered Oct 03 '22 01:10

Alex Feinman


Both operations can be done in a single CPU step, so they should be the same performance wise.

like image 44
Gavin H Avatar answered Oct 03 '22 00:10

Gavin H


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.

like image 37
Phone Guy Avatar answered Oct 03 '22 00:10

Phone Guy


It could be dependent on what operations precede or succeed the comparison. For example, if you assign a value to x just before doing the comparison, then it might be faster to check the sign flag than to compare to a specific value. Or the CPU's branch-prediction performance could be affected by which comparison you choose.

But, as others have said, this is dependent upon CPU architecture, memory architecture, compiler, and a lot of other things, so there is no general answer.

like image 37
Kristopher Johnson Avatar answered Oct 03 '22 01:10

Kristopher Johnson


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.

like image 32
CodexArcanum Avatar answered Oct 03 '22 01:10

CodexArcanum


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.
like image 31
Bob Cross Avatar answered Oct 03 '22 01:10

Bob Cross


Same, both operations are usually done in 1 clock.

like image 37
Artyom Avatar answered Oct 03 '22 00:10

Artyom