9

Which operator is faster: > or ==?

Example: I want to test a value (which can have a positive value or -1) against -1 :

if(time > -1)
// or
if (time != -1)

time has type "int"

Aminos
  • 754
  • 1
  • 20
  • 40
  • May be related: http://stackoverflow.com/questions/8501205/speed-of-c-operators-simple-math – Martin Ba Nov 20 '15 at 11:49
  • @Let_Me_Be Things like this happens on SO often. – Michi Nov 20 '15 at 11:50
  • 1
    It is not only about performance, it's also about safety. If the following code works safely only with non-negative values then it's necessary to use `time > -1`. – dlask Nov 20 '15 at 11:50
  • 3
    You can find out yourself by writing a simple program that tests the two operators and compares their execution times (over a large number of iterations of course). The difference, if any, is probably negligible; I expect both cases would compile to a single assembly instruction. – elnigno Nov 20 '15 at 11:50
  • 1
    Some processors are quicker when comparing against zero. So `> 0` might be faster than `>= 1`, as an example. – Simple Nov 20 '15 at 12:00
  • 1
    @Simple A decent compiler will replace `>= 1` with `> 0` if it's faster. – molbdnilo Nov 20 '15 at 12:08
  • @Let_Me_Be You wrote the very reason for downvotes. Remember, downvote reasons are not the same as closure reasons. One is free to vote as s/he pleases, but closure requires arguments. – this Nov 20 '15 at 13:01
  • [Is < faster than <=?](https://stackoverflow.com/q/12135518/995714), [Which operator is faster (> or >=), (< or <=)?](https://stackoverflow.com/q/11763399/995714), [Is the inequality operator faster than the equality operator?](https://stackoverflow.com/q/1029992/995714) – phuclv Sep 03 '18 at 11:13
  • `0` isn't a positive number. The positive integers are `1, 2, 3, ...`. The non-negative integers are `0, 1, 2, 3, ...`. – Andreas Rejbrand Sep 03 '18 at 12:51
  • Could you please unaccept my answer? I think it's not much helpful practically now. – songyuanyao May 31 '21 at 04:17
  • @songyuanyao OK, done ! I must admit the question is stupid, but I was at the start of my developer career and I wanted to write efficient code. – Aminos May 31 '21 at 09:10
  • @Aminos Thanks! Trying to write efficient code is not a bad thing, definitely. :) Anyway, I'm sure you could still get much knowledges and suggestions here, good luck! – songyuanyao May 31 '21 at 09:26

3 Answers3

20

The standard doesn't say. So it's up to what opcodes the given compiler generates in its given version, and how fast a given CPU executes them.

I.e., implementation / platform defined.

You can find out for a specific compiler / platform combination by looking at / benchmarking the executable code.

But I seriously doubt it will make much of a difference; this is the kind of micro-optimization that is almost always dwarfed by higher-level architectural decisions.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
12

It is platform-dependent. Generally though, those two operations will translate directly to the assembler instructions "branch if greater than" and "branch if not equal". It is unlikely that there is any performance difference between those two, and if there would be, it would be non-significant.

The only branch instruction which is ever so slightly faster than the others is usually "branch if zero"/"branch if not zero".

(In the dark ages when compilers sucked, C programmers therefore liked to write loops as down-counting to zero, instead of up-counting, so that comparisons would be done against zero instead of a value, in order to gain a few nanoseconds. Modern compilers can do that optimization themselves, but you still see such loops now and then.)

In general, you shouldn't concern yourself with micro-management of performance. If you spend time pondering if > is faster than !=, instead of pondering about program design, readability and functionality, you need to set your priorities straight asap.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 3
    Nice answer! I would only like to add that sometimes we do work in a field like raytracing where performance is one of the top competitive standards (besides correctness, of course, but in a raytracer context there usually aren't so many tricky side effects to trip over -- and "correctnesss" is loosely defined there in the pursuit of beautiful images faster). Even then, the pros don't go around asking, "Is this operator faster than that one?" Any kind of micro-optimization is something you zoom into, after profiling and measuring and coarser optimizations -- prioritization is key as stated. –  Nov 20 '15 at 12:36
6

Semantically these conditions are different. The first one checks whether object time is positive or zero.

if(time > -1)

In this case it would be better to write

if( time >= 0 )

However some functions return either a non-negative value or -1. For example a search function can return -1 if it did not find an element in an array. Or -1 can signal an error state or an absence of a value.

In this case it is better to use condition

if ( time != -1 )

As for the speed when the compiler can generate only one mashine instruction to make the comparison in the both cases.

It is not the case when you should think about the speed. You should think about what condition is more expressive and shows the intention of the programmer.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 0 is not a positive number, so (assuming `x` is a signed integer) `x > -1` doesn't test if `x` is positive. It tests if `x` is *non-negative*. – Andreas Rejbrand Sep 03 '18 at 12:47