Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any performance difference between greater than and greater than or equal?

On today's modern processors, is there any performance difference between greater than and greater than or equal comparison for a branch condition? If I have a condition that could just as easily be either, is there any slight advantage to choosing > over >= or vice-versa? (This would be for a compiled language on Intel or AMD hardware)

like image 209
WilliamKF Avatar asked May 30 '11 16:05

WilliamKF


People also ask

What is the difference between greater than and greater than and equal to?

'Greater than' means that some variable or number can have any value that is greater than the given limit. Whereas the 'greater than or equal to' symbol states that the number or variable can be equal to or more than the given limit.

Which is faster == or !=?

What I meant is that the CPU could detect two values are not equal without looking at all bits, but it doesn't matter whether you use == or != to find that they are not equal, so the two operators are exactly equivalent. There is no reason to think one is faster than the other.


2 Answers

There shouldn't be any noticeable difference between comparing different predicates, because of the way they're computed (beware I haven't read the x86 manuals in detail so it may work different):

Most instructions produce several flags as a byproduct, usually you have at least: carry (c), overflow (o), zero (z) and negative (n).

Using those predicates that are created by a x-y instruction (that creates the above 4 reliably) we can easily figure out all wanted comparisions trivially. For unsigned numbers:

x = y    z
x != y   !z
x < y    !c
x <= y   !c + z
x > y    c . !z
x >= y   c

So it hardly makes any difference. But then there are some differences, which mostly come down to the fact if we can use TEST (which is an AND instead of a full blown subtraction) or have to use CMP (that's the subtraction). TEST is more limited but faster (usually).

Also modern architectures (starting from c2d on intel side) can sometimes fuse two µops into one macro op - so called macro-op fusion which has some nice advantages. And the rules for that change from one architecture to the next and are a bit longer. For example branches that test the overflow, parity or sign flag only (JO, JNO, JP, JNP, JS, JNS) can fuse with TEST but not with CMP on c2d and nehalems (you bet I looked that one up - section 7.5).

So can we just say it's complicated and not worry about such things? That is except if you're writing an optimizer for a compiler, because really - independent of WHAT you write in your source code the compiler will do what it wants anyhow - and for good reason (ie if JGE were theoretically faster you'd have to write if (x < y) usually..). And if you really need one advice: Comparing against 0 is often faster.

like image 188
Voo Avatar answered Sep 20 '22 14:09

Voo


I seriously doubt there's a difference.

like image 25
MRAB Avatar answered Sep 17 '22 14:09

MRAB